0
0
Angularframework~10 mins

Defining routes array in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining routes array
Start: Define routes array
Create route objects with path & component
Add route objects to routes array
Pass routes array to RouterModule.forRoot()
Angular reads routes array
Router matches URL to path
Render matched component
End
This flow shows how Angular uses a routes array to map URL paths to components for navigation.
Execution Sample
Angular
import { Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';

export const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];
Defines a routes array with two routes: the home path and the about path, each linked to a component.
Execution Table
StepActionRoutes Array StateRouter Behavior
1Import Routes and components[]No routing yet
2Create route object { path: '', component: HomeComponent }[{ path: '', component: HomeComponent }]Router ready to match '' path
3Create route object { path: 'about', component: AboutComponent }[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]Router ready to match 'about' path
4Export routes array[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]Routes array available for RouterModule
5RouterModule.forRoot(routes) called[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]Router listens to URL changes
6User navigates to '/'[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]Router matches '' path, renders HomeComponent
7User navigates to '/about'[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]Router matches 'about' path, renders AboutComponent
8User navigates to '/contact'[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]No match found, router may show fallback or nothing
💡 Routing stops when URL matches no path or user leaves app.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
routesundefined[{ path: '', component: HomeComponent }][{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }][{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }][{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]
Key Moments - 3 Insights
Why do we use an array of objects for routes instead of a single object?
Because each route needs its own path and component, so we list them as separate objects inside an array, as shown in execution_table rows 2 and 3.
What happens if the user navigates to a path not defined in the routes array?
The router cannot find a matching route, so it may show a fallback page or nothing, as seen in execution_table row 8.
Why is the empty string '' used as a path in the routes array?
The empty string '' represents the default or home path, so when the URL is '/', the router matches this route and renders the HomeComponent, as in execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the routes array after step 3?
A[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]
B[{ path: '', component: HomeComponent }]
C[]
D[{ path: 'about', component: AboutComponent }]
💡 Hint
Check the 'Routes Array State' column at step 3 in execution_table.
At which step does the router start listening to URL changes?
AStep 4
BStep 5
CStep 6
DStep 2
💡 Hint
Look for 'RouterModule.forRoot(routes) called' in the 'Action' column of execution_table.
If we add a route { path: 'contact', component: ContactComponent }, how would the routes array look after step 3?
A[{ path: 'contact', component: ContactComponent }]
B[{ path: '', component: HomeComponent }, { path: 'contact', component: ContactComponent }]
C[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: 'contact', component: ContactComponent }]
D[{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]
💡 Hint
Adding a new route appends it to the routes array as shown in variable_tracker and execution_table.
Concept Snapshot
Defining routes array in Angular:
- Use an array of objects, each with 'path' and 'component'.
- Example: [{ path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }]
- Pass this array to RouterModule.forRoot() to enable routing.
- Router matches URL paths to components to render.
- Empty string path '' means the home page.
Full Transcript
In Angular, we define navigation paths using a routes array. Each route is an object with a 'path' string and a 'component' to show. We create this array step-by-step by adding route objects. Then we export it and pass it to RouterModule.forRoot(). Angular reads this array to match the current URL path and render the correct component. For example, the empty string path '' matches the home page URL '/'. If the user navigates to a path not in the array, no component is shown or a fallback can appear. This process helps Angular know what to show when users move around the app.