We define a routes array to tell Angular which page or component to show when a user visits a specific URL. It helps users move around the app smoothly.
Defining routes array in Angular
Introduction
Syntax
Angular
import { Routes } from '@angular/router'; export const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: '**', component: PageNotFoundComponent } ];
The routes array is an array of objects.
Each object has a path and a component or other properties like redirectTo.
Examples
HomeComponent when the URL is empty (root).Angular
export const routes: Routes = [ { path: '', component: HomeComponent } ];
:id to show different profiles based on the URL.Angular
export const routes: Routes = [ { path: 'profile/:id', component: ProfileComponent } ];
old-path to new-path.Angular
export const routes: Routes = [ { path: 'old-path', redirectTo: 'new-path', pathMatch: 'full' }, { path: 'new-path', component: NewComponent } ];
Angular
export const routes: Routes = [ { path: '**', component: NotFoundComponent } ];
Sample Program
This example defines four routes: 'home' and 'about' show their components, empty path redirects to 'home', and '**' catches unknown paths.
Angular
import { Component } from '@angular/core'; import { Routes } from '@angular/router'; @Component({ selector: 'app-home', template: '<h2>Home Page</h2>' }) export class HomeComponent {} @Component({ selector: 'app-about', template: '<h2>About Page</h2>' }) export class AboutComponent {} @Component({ selector: 'app-not-found', template: '<h2>Page Not Found</h2>' }) export class PageNotFoundComponent {} export const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: '**', component: PageNotFoundComponent } ];
Important Notes
Routes are matched in order. The first match is used.
Use pathMatch: 'full' when redirecting from empty path to avoid partial matches.
Wildcard '**' should be last to catch all unmatched routes.
Summary
Routes array tells Angular which component to show for each URL path.
Each route has a path and a component or redirectTo.
Order matters: specific routes first, wildcard last.
Practice
1. What does the
routes array in Angular define?easy
Solution
Step 1: Understand the purpose of routes array
The routes array maps URL paths to components so Angular knows what to display.Step 2: Eliminate unrelated options
Styles, services, and database connections are not handled by routes.Final Answer:
It tells Angular which component to show for each URL path. -> Option CQuick Check:
Routes array = component mapping [OK]
Hint: Routes array links URL paths to components [OK]
Common Mistakes:
- Confusing routes with styles or services
- Thinking routes handle backend connections
- Assuming routes define component logic
2. Which of the following is the correct way to define a route for path 'home' with component
HomeComponent?easy
Solution
Step 1: Check correct property names
The route object must havepathandcomponentproperties.Step 2: Verify component value type
The component value should be the component class, not a string or redirect.Final Answer:
{ path: 'home', component: HomeComponent } -> Option AQuick Check:
Use path and component keys correctly [OK]
Hint: Use component class, not string or redirectTo for components [OK]
Common Mistakes:
- Using 'route' instead of 'path'
- Setting component as a string
- Using redirectTo instead of component for components
3. Given this routes array:
What component will Angular show when the URL path is '/contact'?
const routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '**', component: NotFoundComponent }
];What component will Angular show when the URL path is '/contact'?
medium
Solution
Step 1: Match URL path to routes
Path '/contact' does not match '' or 'about'.Step 2: Use wildcard '**' route
The '**' path catches all unmatched URLs and shows NotFoundComponent.Final Answer:
NotFoundComponent -> Option DQuick Check:
Wildcard '**' = NotFoundComponent [OK]
Hint: Unmatched paths go to '**' route component [OK]
Common Mistakes:
- Assuming empty path matches all URLs
- Thinking unmatched paths cause errors
- Confusing wildcard '**' with empty path
4. Identify the error in this routes array:
What is wrong with the route for 'profile'?
const routes = [
{ path: '', component: HomeComponent },
{ path: 'profile', redirectTo: 'user' },
{ path: 'user', component: UserComponent }
];What is wrong with the route for 'profile'?
medium
Solution
Step 1: Check redirectTo usage
When using redirectTo, Angular requires apathMatchproperty to specify matching strategy.Step 2: Confirm missing pathMatch causes error
Without pathMatch, Angular cannot decide when to redirect, causing routing errors.Final Answer:
redirectTo should be used with pathMatch property. -> Option AQuick Check:
redirectTo needs pathMatch [OK]
Hint: Always add pathMatch with redirectTo routes [OK]
Common Mistakes:
- Omitting pathMatch with redirectTo
- Thinking redirectTo needs component
- Assuming path names must be capitalized
5. You want to define routes so that:
- The home page shows
- The about page shows
- Any unknown path redirects to home.
Which routes array correctly implements this?
- The home page shows
HomeComponent at path ''.- The about page shows
AboutComponent at path 'about'.- Any unknown path redirects to home.
Which routes array correctly implements this?
hard
Solution
Step 1: Check wildcard path for unknown URLs
The wildcard path for unknown URLs is '**', not '*'.Step 2: Confirm redirectTo usage with pathMatch
Redirect routes must includepathMatch: 'full'to work properly.Step 3: Verify redirect target and components
Redirecting '**' to '' sends unknown URLs to home page correctly.Final Answer:
[ { path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '**', redirectTo: '', pathMatch: 'full' } ] -> Option BQuick Check:
Use '**' with redirectTo and pathMatch 'full' [OK]
Hint: Use '**' wildcard with redirectTo and pathMatch 'full' [OK]
Common Mistakes:
- Using '*' instead of '**' for wildcard
- Omitting pathMatch in redirectTo routes
- Using component instead of redirectTo for redirects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
