Angular Routing • Nested Layouts • Params
Nested routes let you build a hierarchy of views inside a parent route (for example, an Admin area with its own pages). Dynamic routes allow you to pass parameters (like /products/42) so each nested view can display data based on the URL. Together, they help you create scalable layouts with multiple levels of routing.
children array inside a parent route.:id) that can be read in a component.<router-outlet> in a parent component is where its child routes are rendered.ActivatedRoute is used to access dynamic parameters in nested components.paramMap lets your component react when the route parameters change.To define nested routes, configure a parent path with a component and provide its child routes using the children property. The parent route becomes a wrapper layout for the nested views.
// app-routing.module.ts - nested admin routes
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin/admin.component';
import { DashboardComponent } from './admin/dashboard/dashboard.component';
import { SettingsComponent } from './admin/settings/settings.component';
const routes: Routes = [
{ path: 'admin', component: AdminComponent,
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'settings', component: SettingsComponent }
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
When your nested route includes a parameter (for example, /products/:id), you can use ActivatedRoute to read that parameter in the child component. Using paramMap ensures your component updates whenever the URL changes.
// product-detail.component.ts - reading a dynamic route param
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-product-detail',
template: '<p>Product ID: {{ productId }}</p>'
})
export class ProductDetailComponent implements OnInit {
productId!: string;
constructor(private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.paramMap.subscribe(params => {
this.productId = params.get('id')!;
});
}
}
router-outlet for Nested ViewsThe parent component of a nested route must contain a <router-outlet>. This is where the current child route component (like Dashboard or Settings) will be rendered.
<!-- admin.component.html - layout for nested admin child routes -->
<h2>Admin Panel</h2>
<nav>
<a routerLink="dashboard">Dashboard</a> |
<a routerLink="settings">Settings</a>
</nav>
<router-outlet></router-outlet>
/admin/dashboard.AdminComponent loads (parent route), displaying the heading and navigation menu.dashboard is matched and DashboardComponent is rendered inside the parent’s <router-outlet>./products/42, the ProductDetailComponent reads the id parameter using ActivatedRoute and shows “Product ID: 42”.This pattern lets you build sections like /admin, /user, or /shop with their own internal pages and dynamic content, while keeping your routing clean and organized.
/admin → /admin/dashboard, /admin/settings).<router-outlet> in the parent component of nested routes.paramMap instead of relying only on snapshot for dynamic parameters./user section with nested routes for profile, settings, and history./user/:id/profile and display the current user ID in the profile component.routerLink to navigate between the nested user routes and observe how the content changes inside a single layout./admin/reports/sales and /admin/reports/users.