Child routes and layout composition
Nesting routes inside a parent's own layout, and using named outlets to render more than one routed region at once.
Child routes and layout composition
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A `children` array nests routes under a parent, and the parent's own template needs its own `<router-outlet>` to render whichever child matched — the root outlet alone only renders the top-level route.
- Nesting isn't just a URL-structure convenience — it's how a shared layout (a sidebar, a persistent header) stays mounted while only the inner content swaps as child routes change.
- A named outlet (`<router-outlet name="sidebar">`) lets more than one region of the page be independently routed at once, each matched by its own part of the route configuration.
Nesting a route is really nesting a layout
A flat list of routes works fine until a group of pages needs to share
something — a sidebar, a tab bar, a persistent header specific to one section
of the app. /settings/profile and /settings/billing are naturally two
separate pages, but they almost certainly share a settings-section layout
around them. Child routes exist to express exactly that relationship: not just
“these URLs are related,” but “these pages render inside a shared parent
layout that stays mounted across all of them.”
const routes: Routes = [
{
path: 'settings',
component: SettingsShellComponent,
children: [
{ path: 'profile', component: ProfileComponent },
{ path: 'billing', component: BillingComponent },
],
},
];
<!-- SettingsShellComponent's own template -->
<nav>
<a routerLink="profile">Profile</a>
<a routerLink="billing">Billing</a>
</nav>
<router-outlet></router-outlet>
This is the detail that catches people the first time: the root <router-outlet>
in the app’s top-level template renders SettingsShellComponent for the
settings path — but SettingsShellComponent needs its own
<router-outlet>, inside its own template, for its children to have
somewhere to render into. Navigate to /settings/profile, and the root outlet
shows SettingsShellComponent, whose own nested outlet then shows
ProfileComponent inside it. The sidebar in SettingsShellComponent’s template
stays mounted the whole time a user clicks between profile and billing —
only the nested outlet’s content swaps.
Named outlets: more than one routed region at once
A single page occasionally needs more than one independently routable region — a main content area and a persistent side panel that can each show different things based on the URL, without one containing the other the way a parent-child route relationship does. A named outlet is the mechanism for that:
<router-outlet></router-outlet>
<router-outlet name="sidebar"></router-outlet>
{ path: 'inbox', component: InboxComponent, outlet: 'sidebar' }
The default outlet (unnamed) and a named sidebar outlet can each be matched by
their own part of the route configuration simultaneously — the URL ends up
encoding both (“what’s in the main area” and “what’s in the sidebar”) at once,
in what’s called an auxiliary route. This is a genuinely less common pattern
than parent-child nesting, and worth recognizing when you see it rather than
reaching for by default — most “shared layout” needs are better served by the
nested-route approach above, with named outlets reserved for cases where two
regions truly need independent routing rather than one containing the other.
Navigating relative to where you already are
Nesting introduces a navigation wrinkle worth knowing about: a routerLink
written inside SettingsShellComponent’s own template, like
routerLink="billing" (no leading slash), is relative to that component’s own
route, not the app’s root — it resolves to /settings/billing automatically,
without needing to know or repeat the full settings/ prefix. Navigating
programmatically needs the same relative context supplied explicitly:
this.router.navigate(['billing'], { relativeTo: this.route });
Without relativeTo, router.navigate(['billing']) would instead resolve
against the root, almost certainly landing somewhere you didn’t intend. This
is a common early mistake specifically because it only surfaces once routes
are actually nested — a flat, unnested route configuration never exposes the
difference between relative and absolute navigation at all.
A note on this lesson’s video
This lesson’s video covers nested child routes directly and is genuinely useful for that half of the topic. Named-outlet, multi-region layout composition specifically is thin ground in video form across the whole web right now — the prose above is written to cover that half on its own rather than leave a gap. If you want to see named outlets in action rather than just read about them, Angular’s own documentation on auxiliary routes is the most reliable reference available at the time of writing.
Check yourself
A route is configured as `{ path: 'settings', component: SettingsShellComponent, children: [{ path: 'profile', component: ProfileComponent }] }`. What does `SettingsShellComponent`'s own template need for `/settings/profile` to actually render `ProfileComponent`?
Its own `<router-outlet>`. The root outlet in the app's top-level template only renders the matched top-level route — here, `SettingsShellComponent` itself. Rendering `ProfileComponent` as the matched *child* route requires a second, nested outlet inside `SettingsShellComponent`'s own template, specifically for its children to render into.
Why would a settings page use nested routes with a shared `SettingsShellComponent`, instead of each settings sub-page being its own independent top-level route?
Nesting keeps `SettingsShellComponent` — its sidebar navigation, its header, whatever layout it provides — mounted once and shared across every child route, with only the inner content swapping as the user navigates between sub-pages. Independent top-level routes would each need to re-declare and re-render that shared layout themselves, with no structural relationship tying them together as parts of the settings section.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.