Sub topic 3
Rendering Routed Components with RouterOutlet
This chapter assumes you know standalone components, template syntax, inject(), and the route table built with Routes and provideRouter in the previous chapter. In this chapter you learn where routed components actually appear on the page and how the router puts them there. You also learn how to run more than one outlet and which mistakes usually leave the page blank.
What RouterOutlet does
RouterOutlet is a directive that marks a placeholder in a template. When a navigation finishes, the router works out which route matched each segment of the URL. It then asks the right outlet to create the matching component. When the URL changes again, the outlet destroys the old component and creates the new one.
The outlet holds no state of its own about which component to show. The router keeps a tree of outlet contexts, one for each level of the matched route tree. Each <router-outlet> registers itself with the context at its level when it is created, and the router tells it to activate or deactivate.
Placing the outlet in the root component
The usual setup puts one primary outlet in the root component and keeps the navigation, header, and footer around it. Those parts stay on screen across navigations. Only the outlet's content changes.
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink],
template: `
<header>
<a routerLink="/home">Home</a>
<a routerLink="/products/42">Product 42</a>
</header>
<main>
<router-outlet
(activate)="onActivate($event)"
(deactivate)="onDeactivate($event)" />
</main>
<footer>Shop footer</footer>
`
})
export class AppComponent {
onActivate(component: object): void {
console.log('Activated', component.constructor.name);
}
onDeactivate(component: object): void {
console.log('Deactivated', component.constructor.name);
}
}RouterOutlet must be in the component's imports array, just like any other standalone directive. The self-closing form <router-outlet /> is valid in Angular 16 and later. <router-outlet></router-outlet> works in every version.
The component is a sibling, not a child
The outlet does not put the routed component inside itself. It inserts the component's host element directly after the <router-outlet> element. The rendered DOM looks like this:
<main>
<router-outlet></router-outlet>
<app-product-detail>...</app-product-detail>
</main>This affects CSS. A selector like router-outlet > * matches nothing. Use router-outlet + * to target the routed element, or put the styles on the routed component's host with :host.
Outlet events and the exported API
The outlet emits four outputs:
| Output | Fires when | Payload |
|---|---|---|
| `activate` | A new component instance is created in the outlet | The component instance |
| `deactivate` | The current component is destroyed or replaced | The component instance |
| `attach` | A stored component is reattached by a custom `RouteReuseStrategy` | The component instance |
| `detach` | A component is detached and stored for later reuse | The component instance |
activate is useful for small cross-cutting jobs, such as scrolling a container to the top or moving keyboard focus to the new view for accessibility. Avoid reading or changing the routed component's state through the payload. That couples the parent to every routed child.
The directive is exported as outlet, so you can take a template reference and read its state:
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-shell',
standalone: true,
imports: [RouterOutlet],
template: `
<router-outlet #outlet="outlet" />
@if (outlet.isActivated) {
<p>Showing route: {{ outlet.activatedRoute.snapshot.url.join('/') }}</p>
}
`
})
export class ShellComponent {}Reading outlet.component or outlet.activatedRoute while isActivated is false throws an error. Always check isActivated first.
Component lifecycle: destroyed or reused
Two navigations look alike but behave differently:
- Going from
/hometo/products/42matches a different route config. The outlet destroysHomeComponent(itsngOnDestroyruns) and createsProductDetailComponentfrom scratch. - Going from
/products/42to/products/43matches the same route config. The defaultRouteReuseStrategykeeps the existingProductDetailComponentinstance. Only the route parameters change.
The second case causes a common bug. Code that reads the id once in ngOnInit or the constructor shows stale data, because ngOnInit does not run again. Read the parameter reactively instead. Use an @Input() with withComponentInputBinding(), which updates the input on every navigation, or subscribe to ActivatedRoute.paramMap. The route parameters chapter covers both approaches.
Named outlets
A template can have one primary outlet (no name) and any number of named outlets at the same level. Named outlets show independent content side by side, such as a help panel or a chat drawer that stays open while the main view changes.
// app.routes.ts
import { Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { HelpPanelComponent } from './help-panel.component';
export const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'help', component: HelpPanelComponent, outlet: 'sidebar' },
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];// app.component.ts
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink],
template: `
<a [routerLink]="[{ outlets: { sidebar: ['help'] } }]">Open help</a>
<a [routerLink]="[{ outlets: { sidebar: null } }]">Close help</a>
<div class="layout">
<main><router-outlet /></main>
<aside><router-outlet name="sidebar" /></aside>
</div>
`
})
export class AppComponent {}When help is open, the URL becomes /home(sidebar:help). The part in parentheses is called a secondary route. Each named outlet appears there as outletName:path. Setting the outlet to null removes its segment and destroys its component. The primary outlet is left alone.
Some rules apply to named outlets:
- A route with
outlet: 'sidebar'only matches for an outlet with that exact name. It never renders in the primary outlet. - Named outlet names must be unique within one template level. Two outlets with the same name at the same level conflict, and only one of them receives the component.
- Secondary routes must have a non-empty
pathto be reachable from a URL. Lazy routes inside them work too, but keep them simple. Deep trees in secondary outlets make URLs hard to read and hard to share.
Passing data into the routed component
Because the router creates the component, you cannot bind @Input() values on it in the template. Since Angular 19, the outlet accepts a routerOutletData input. The routed component, and any component below it, reads that value as a signal through the ROUTER_OUTLET_DATA token.
// app.component.ts
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `<router-outlet [routerOutletData]="theme()" />`
})
export class AppComponent {
theme = signal<'light' | 'dark'>('dark');
}// home.component.ts
import { Component, Signal, inject } from '@angular/core';
import { ROUTER_OUTLET_DATA } from '@angular/router';
@Component({
selector: 'app-home',
standalone: true,
template: `<p>Current theme: {{ theme() }}</p>`
})
export class HomeComponent {
theme = inject(ROUTER_OUTLET_DATA) as Signal<'light' | 'dark'>;
}Use this for layout-level values that the parent owns. Put values that belong in the URL into the URL. Use services for shared application state.
Nested outlets in brief
A routed component can have its own <router-outlet>. That outlet renders the component's child routes, one level deeper in the route tree. Each outlet only knows about its own level. This is why a child route never appears in the root outlet. The chapter on child routes builds full nested layouts.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| `'router-outlet' is not a known element` | `RouterOutlet` is missing from the standalone component's `imports` | Add `RouterOutlet` to `imports` |
| `NullInjectorError` naming a router service such as `ChildrenOutletContexts` | The router was never provided | Add `provideRouter(routes)` to the application providers |
| URL changes but the page is blank | No outlet at that level, or the route targets a named outlet that does not exist | Add the outlet, or check the `outlet` name on the route |
| Component appears late | The outlet is inside an `@if` or `@defer` block that renders after navigation | Accept it, or render the outlet unconditionally |
| Data does not update between `/products/1` and `/products/2` | Component reuse, with parameters read only once | Read parameters reactively |
Exercise
Your global stylesheet has router-outlet > app-product-detail { padding: 1rem; }, but the product page has no padding. Why, and how do you fix it?
Show answer
RouterOutlet inserts the routed component as the next sibling of the <router-outlet> element, not as its child, so the child combinator matches nothing. Use router-outlet + app-product-detail or router-outlet + *, or move the rule into the component's own styles as :host { display: block; padding: 1rem; }.
Exercise
The app shows /home in the primary outlet and HelpPanelComponent in an outlet named sidebar. Write the routerLink array that closes the sidebar without changing the primary route, and give the URL before and after.
Show answer
Use [routerLink]="[{ outlets: { sidebar: null } }]". Before, the URL is /home(sidebar:help). After, it is /home. The sidebar component is destroyed and the primary outlet keeps HomeComponent.
Exercise
A ProductDetailComponent loads its product in ngOnInit using route.snapshot.paramMap.get('id'). Navigating from /products/1 to /products/2 still shows product 1. Explain why and name two fixes.
Show answer
Both URLs match the same route config, so the default RouteReuseStrategy reuses the component instance and ngOnInit does not run again. The snapshot was read only once. Fix it by subscribing to route.paramMap, which emits on every parameter change, or by enabling withComponentInputBinding() and reacting to an id input (for example with a setter, ngOnChanges, or a signal input with a computed or effect).