Sub topic 3
Rendering Routed Components with RouterOutlet
This chapter assumes you know standalone components, Angular templates, and the route table from the previous chapter. In that chapter, the router matched a URL to a component. This chapter covers where that component appears on the page.
What RouterOutlet Does
RouterOutlet is a directive that marks a spot in a template. When a navigation finishes, the router creates the matched component and inserts it at that spot. When the URL changes to a different route, the router destroys the old component and inserts the new one.
The component is inserted as a sibling placed directly after the <router-outlet> element, not inside it. If you inspect the DOM, <router-outlet> is empty and <app-home> comes right after it. Keep this in mind when you write CSS selectors such as router-outlet > *, because they will match nothing.
Placing the Outlet in a Layout
The root component usually holds shared layout, such as a header, navigation, and footer, with a single outlet in the middle. Everything outside the outlet stays on screen across navigations.
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `
<header>My Shop</header>
<main>
<router-outlet
(activate)="onActivate($event)"
(deactivate)="onDeactivate($event)" />
</main>
<footer>© 2026</footer>
`
})
export class AppComponent {
onActivate(component: object): void {
console.log('Activated:', component.constructor.name);
}
onDeactivate(component: object): void {
console.log('Deactivated:', component.constructor.name);
}
}The activate and deactivate outputs emit the component instance each time the router swaps views. They are handy for logging or for scrolling a container back to the top. Avoid using them to pass data into the routed component, because route parameters and resolvers do that job better.
Named Outlets
A template can have more than one outlet. The unnamed one is the primary outlet. Any others need a name, and routes target them with the outlet property:
// in the route table
{ path: 'help', component: HelpPanelComponent, outlet: 'aside' }// in a template
// <router-outlet />
// <router-outlet name="aside" />The URL /home(aside:help) renders HomeComponent in the primary outlet and HelpPanelComponent in the aside outlet. Named outlets are useful for side panels or dialogs that need their own URL state. Most apps only need the primary outlet.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `'router-outlet' is not a known element` | `RouterOutlet` is missing from the standalone component's `imports` | Add `RouterOutlet` to `imports` |
| URL changes but nothing renders | No outlet in the template, or the outlet sits inside an `@if` that is false | Put the outlet where it is always present |
| Component state is lost after navigating away | The router destroys the component when its route deactivates | Keep shared state in a service |
| Styles on `router-outlet > *` have no effect | The routed component is a sibling of the outlet, not a child | Style the wrapper element or the component host |
Exercises
Exercise
You add <router-outlet /> to a standalone AppComponent template and the app fails to compile with "'router-outlet' is not a known element". What is missing?
Show answer
RouterOutlet must be listed in the component's imports array, for example imports: [RouterOutlet], with RouterOutlet imported from '@angular/router'.
Exercise
In the browser's DOM inspector, where does the routed component's host element appear relative to the router-outlet element?
Show answer
It appears as the next sibling directly after the router-outlet element. The router-outlet element itself stays empty.