0
0
Angularframework~10 mins

Child routes and nested routing in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Child routes and nested routing
App starts
Router reads URL
Match parent route
Render parent component
Check for child route
Yes
Match child route
Render child component inside parent
Display nested view
User navigates -> Repeat
The router first matches the parent route, renders its component, then checks and renders the child route component inside the parent's view.
Execution Sample
Angular
const routes = [
  { path: 'home', component: HomeComponent, children: [
    { path: 'profile', component: ProfileComponent }
  ]}
];
Defines a parent route 'home' with a nested child route 'profile' that renders inside HomeComponent.
Execution Table
StepURLRouter ActionComponent RenderedView Location
1/homeMatch 'home' routeHomeComponentMain outlet
2/homeCheck for child routeNone yetNo nested view rendered
3/home/profileMatch 'home' routeHomeComponentMain outlet
4/home/profileMatch child 'profile' routeProfileComponentInside HomeComponent's router-outlet
5/home/profileDisplay nested viewHomeComponent + ProfileComponentParent + nested outlet
6/aboutNo match in 'home' routeNoneNo components rendered
💡 Routing stops when no matching route is found or URL fully matched.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
Current URL''/home/home/profile/home/profile/home/profile
Matched Parent Routenullhomehomehomehome
Matched Child Routenullnullprofileprofileprofile
Rendered ComponentsnoneHomeComponentHomeComponentHomeComponent + ProfileComponentHomeComponent + ProfileComponent
Key Moments - 3 Insights
Why does the child component render inside the parent component's view?
Because the parent component's template includes a <router-outlet> where the child route's component is inserted, as shown in steps 4 and 5 of the execution_table.
What happens if the URL matches only the parent route but no child route?
Only the parent component renders and no nested child component appears, as seen in step 2 where no child route matched yet.
Why does navigation to a non-matching URL render no components?
Because the router cannot find any route matching the URL, so it renders nothing, as shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what component(s) are rendered at step 3 when URL is '/home/profile'?
AOnly HomeComponent
BOnly ProfileComponent
CHomeComponent and ProfileComponent
DNo components rendered
💡 Hint
Check the 'Component Rendered' column at step 3 in the execution_table.
At which step does the router render the child component inside the parent?
AStep 2
BStep 4
CStep 1
DStep 6
💡 Hint
Look for when 'ProfileComponent' appears in the 'Component Rendered' column.
If the URL was '/home/settings' and no child route 'settings' exists, what would happen?
ANo components render
BBoth HomeComponent and SettingsComponent render
COnly HomeComponent renders
DProfileComponent renders
💡 Hint
Refer to how unmatched child routes behave in the execution_table and key_moments.
Concept Snapshot
Child routes let you nest routes inside a parent route.
Parent component renders first.
Child routes render inside parent's <router-outlet>.
URL matches parent then child.
If no child matches, only parent shows.
Use 'children' array in route config.
Full Transcript
When Angular starts routing, it reads the URL and tries to match the parent route first. If it finds a parent route, it renders that component. Then it checks if the URL has a child route segment. If yes, it matches the child route and renders the child component inside the parent's router-outlet. This nesting allows building complex layouts with components inside components. If the URL matches only the parent route, only the parent component shows. If no route matches, no components render. This flow repeats on every navigation.