Child routes let you organize your app's pages inside other pages. It helps keep things neat and easy to navigate.
Child routes and nested routing in Angular
const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ];
The children array holds the nested routes inside a parent route.
The parent component should have a <router-outlet> where child routes show up.
const routes: Routes = [ { path: 'dashboard', component: DashboardComponent, children: [ { path: 'stats', component: StatsComponent }, { path: 'reports', component: ReportsComponent } ] } ];
const routes: Routes = [ { path: 'products', component: ProductsComponent, children: [ { path: '', component: ProductListComponent }, { path: ':id', component: ProductDetailComponent } ] } ];
This example shows a parent page with two child pages. The parent has links to switch between child1 and child2. The child content appears inside the parent's <router-outlet>.
When you open the app, it redirects to 'parent/child1' by default.
import { Component } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; @Component({ selector: 'app-parent', template: ` <h2>Parent Page</h2> <nav> <a routerLink="child1" aria-label="Go to Child 1">Child 1</a> | <a routerLink="child2" aria-label="Go to Child 2">Child 2</a> </nav> <router-outlet></router-outlet> ` }) export class ParentComponent {} @Component({ selector: 'app-child1', template: `<p>This is Child 1 content.</p>` }) export class Child1Component {} @Component({ selector: 'app-child2', template: `<p>This is Child 2 content.</p>` }) export class Child2Component {} @Component({ selector: 'app-root', template: `<router-outlet></router-outlet>` }) export class AppComponent {} const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] }, { path: '', redirectTo: 'parent/child1', pathMatch: 'full' } ]; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; @NgModule({ declarations: [AppComponent, ParentComponent, Child1Component, Child2Component], imports: [BrowserModule, RouterModule.forRoot(routes)], bootstrap: [AppComponent] }) export class AppModule {}
Always put <router-outlet> in the parent component template to show child routes.
Use routerLink for navigation links to keep the app fast and smooth.
Child routes help keep URLs clean and show the app structure clearly.
Child routes let you nest pages inside other pages for better organization.
The parent component needs a <router-outlet> to display child content.
Use child routes to create layouts with changing parts and clear URLs.
Practice
Solution
Step 1: Understand child routes concept
Child routes allow nesting routes inside a parent route to organize pages hierarchically.Step 2: Compare options with concept
Only To nest pages inside other pages for better organization correctly describes this purpose; others are incorrect or unrelated.Final Answer:
To nest pages inside other pages for better organization -> Option DQuick Check:
Child routes = Nested pages [OK]
- Thinking child routes replace main router outlet
- Believing child routes work without URLs
- Confusing child routes with disabling navigation
Solution
Step 1: Recall child route syntax
Child routes are defined inside a 'children' array property of a route object.Step 2: Match syntax with options
{ path: 'parent', children: [{ path: 'child', component: ChildComponent }] } correctly uses 'children' with an array of child route objects; others misuse properties or structure.Final Answer:
{ path: 'parent', children: [{ path: 'child', component: ChildComponent }] } -> Option CQuick Check:
Child routes use 'children' array [OK]
- Using 'child' instead of 'children'
- Defining child path as 'parent/child' in one route
- Assigning component directly to 'children'
{ path: 'dashboard', component: DashboardComponent, children: [ { path: 'stats', component: StatsComponent } ] }What happens when navigating to
/dashboard/stats?Solution
Step 1: Understand nested route rendering
Child routes render their components inside the parent's<router-outlet>.Step 2: Apply to given URL
At/dashboard/stats, DashboardComponent loads first, then StatsComponent loads inside its router-outlet.Final Answer:
DashboardComponent loads with StatsComponent displayed inside its router-outlet -> Option BQuick Check:
Child route renders inside parent component [OK]
- Expecting child component to replace parent
- Forgetting to add router-outlet in parent
- Assuming navigation error without cause
{ path: 'home', component: HomeComponent, children: { path: 'profile', component: ProfileComponent } }Solution
Step 1: Check 'children' property type
The 'children' property must be an array of route objects, not a single object.Step 2: Verify given config
Here, 'children' is incorrectly assigned an object instead of an array.Final Answer:
'children' should be an array, not an object -> Option AQuick Check:
'children' = array of routes [OK]
- Using object instead of array for children
- Confusing 'path' usage inside children
- Assuming missing component causes this error
/settings loads SettingsComponent and /settings/profile loads ProfileComponent inside it. Which setup is correct?Solution
Step 1: Setup nested routes properly
To nest ProfileComponent inside SettingsComponent, use 'children' array with ProfileComponent route.Step 2: Ensure parent template has router-outlet
SettingsComponent template must include<router-outlet>to display child routes.Final Answer:
Define { path: 'settings', component: SettingsComponent, children: [{ path: 'profile', component: ProfileComponent }] } and add <router-outlet> in SettingsComponent template -> Option AQuick Check:
Nested route + router-outlet = correct setup [OK]
- Defining nested path as one string without children
- Swapping parent and child components
- Not adding router-outlet in parent component
Ready to prove it?
Pass the test and this pattern moves unknown
