Child Routes & Nested Navigation
Nest routes inside other routes to build complex layouts with nested navigation.
Child routes are routes defined inside a parent route. The parent component has its OWN <router-outlet> where child components render.
"Child routes = ghar ke andar kamre โ parent ghar hai, child kamra hai."
In a typical dashboard layout:
/dashboardโ DashboardComponent (parent with sidebar + outlet)/dashboard/ordersโ OrdersComponent (renders inside dashboard's outlet)/dashboard/profileโ ProfileComponent (renders inside dashboard's outlet)
The parent route acts as a layout container โ its template provides the common UI (sidebar, header) and the child route fills the outlet.
Child routes are defined using the children property in the route config.
const routes: Routes = [{
path: 'dashboard',
component: DashboardComponent,
children: [
{ path: '', component: DashboardHomeComponent },
{ path: 'orders', component: OrdersComponent },
{ path: 'profile', component: ProfileComponent },
]
}];
IMPORTANT: DashboardComponent MUST have a <router-outlet> in its template. Children don't render in the app's main outlet โ they render in the parent's outlet.
"Parent ke andar router-outlet lagao โ child wahan aayega."
With nested routes, you end up with multiple <router-outlet> elements at different levels.
<!-- app.component.html -->
<header>...</header>
<router-outlet /> <!-- Level 1: App-level pages -->
<footer>...</footer>
<!-- dashboard.component.html -->
<div class="dashboard-layout">
<aside class="sidebar">...</aside>
<main>
<router-outlet /> <!-- Level 2: Dashboard children -->
</main>
</div>
"Jitne router-outlets utne levels โ matryoshka dolls jaisa."
Level 1 outlet renders top-level pages (Dashboard, Menu, About). Level 2 outlet (inside Dashboard) renders child pages (Orders, Profile). You can nest as many levels as you need.
When navigating between child routes, you can use relative paths instead of absolute ones.
// From OrdersComponent (/dashboard/orders)
// Navigate to profile sibling (/dashboard/profile)
import { inject } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
export class OrdersComponent {
private router = inject(Router);
private route = inject(ActivatedRoute);
goToProfile() {
// Relative navigation โ '..' means go up one level
this.router.navigate(['../profile'], { relativeTo: this.route });
}
goHome() {
// Absolute navigation โ always from root
this.router.navigate(['/dashboard']);
}
}
"Relative = directions โ left se ya seedha, relative to where you are."
../ goes up one level (from /dashboard/orders to /dashboard). ./ stays at the current level. Without relativeTo, paths are absolute (from root).
An empty child route (path: '') defines what shows when the parent URL has no child path.
{ path: 'dashboard', component: DashboardComponent, children: [
{ path: '', component: DashboardHomeComponent }, // /dashboard
{ path: 'orders', component: OrdersComponent }, // /dashboard/orders
]
}
"Empty path = default kamra โ jab specific kamra nahi bataya."
Without the empty path child, navigating to /dashboard shows the DashboardComponent with an EMPTY child outlet โ a blank area where children should appear. Always include a default child route for parent layouts.
Key Takeaways
- โ Child routes nest inside parent routes using the children: [] property
- โ
Parent component MUST have its own
for children to render - โ Multiple router-outlets create nesting levels: app outlet > dashboard outlet > ...
- โ Relative navigation (../) navigates relative to current route, not from root
- โ Empty child route (path: '') provides default content when no child path specified
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login