Sub topic 9
Building Nested Layouts with Child Routes
This chapter assumes you can define a Routes array, register it with provideRouter, and place a RouterOutlet in a template. Child routes build on all three.
Many screens share a frame. A settings area might keep a sidebar visible while its content panel switches between Profile and Security. With child routes, the parent route renders that frame, and each child renders inside it.
The Working Model
A route can have a children array. When the URL matches a parent path plus a child path, the router builds a tree of components:
- The root
<router-outlet />inAppComponentrenders the parent component. - The parent component's own
<router-outlet />renders the matched child.
Child paths are relative to the parent. A child with path: 'profile' under a parent with path: 'settings' matches /settings/profile.
Defining the Parent and Its Children
// settings.components.ts
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-profile',
standalone: true,
template: `<h3>Profile</h3>`
})
export class ProfileComponent {}
@Component({
selector: 'app-security',
standalone: true,
template: `<h3>Security</h3>`
})
export class SecurityComponent {}
@Component({
selector: 'app-settings',
standalone: true,
imports: [RouterOutlet, RouterLink, RouterLinkActive],
template: `
<aside>
<a routerLink="profile" routerLinkActive="active">Profile</a>
<a routerLink="security" routerLinkActive="active">Security</a>
</aside>
<section>
<router-outlet />
</section>
`
})
export class SettingsComponent {}// app.routes.ts
import { Routes } from '@angular/router';
import { SettingsComponent, ProfileComponent, SecurityComponent } from './settings.components';
export const routes: Routes = [
{
path: 'settings',
component: SettingsComponent,
children: [
{ path: '', redirectTo: 'profile', pathMatch: 'full' },
{ path: 'profile', component: ProfileComponent },
{ path: 'security', component: SecurityComponent }
]
}
];Notice three details:
- The links in
SettingsComponenthave no leading slash.routerLink="profile"resolves relative to the route that renders the component, so it produces/settings/profile. - The empty-path child redirects
/settingsto/settings/profile, so the panel is never blank. - Each child component renders only its own content. The sidebar belongs to the parent.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| Parent renders but the child never appears | The parent template has no `<router-outlet />` | Add an outlet to the parent and import `RouterOutlet` |
| Child route never matches | The child path starts with `/`, such as `'/profile'` | Child paths are relative, so remove the slash |
| `/settings` redirects to `/profile` | The `redirectTo` in a child uses an absolute path | Use `'profile'`, not `'/profile'` |
| Sidebar link goes to `/profile` | The link uses `routerLink="/profile"` | Use a relative link or the full `/settings/profile` |
| Redirect loops or matches too much | An empty-path redirect without `pathMatch: 'full'` | Add `pathMatch: 'full'` |
Exercise
You add a child route { path: 'billing', component: BillingComponent } under settings. Navigating to /settings/billing shows the sidebar but no billing content, and there is no console error. The other children work. What is the most likely cause?
Show answer
BillingComponent's template is probably empty or failing to render. Every child shares the same parent outlet, and that outlet works for the other children, so the outlet is not the problem. Check that the route really is inside the children array and that BillingComponent has a template. If the route were outside children, the URL would not match at all and the router would raise a navigation error.
Exercise
Write a route config where /account/orders and /account/addresses share the /account prefix and an authGuard, with no layout component.
Show answer
Use a componentless parent: { path: 'account', canActivate: [authGuard], children: [{ path: 'orders', component: OrdersComponent }, { path: 'addresses', component: AddressesComponent }] }. Because the parent has no component, each child renders straight into the root outlet.