Full answer
Lazy loaded routes, guards and resolvers take time. Users should see that something is happening.
The Router has an events observable. The main events for one navigation are:
NavigationStart: navigation begins.RoutesRecognized,GuardsCheckStart,GuardsCheckEnd,ResolveStart,ResolveEnd: the stages in between.- One of three endings:
NavigationEnd(success),NavigationCancel(a guard returned false or a newer navigation replaced it) orNavigationError(an error, such as a chunk that failed to load).
To show a spinner:
- Filter the events to the four kinds, and map them to a boolean:
truefor start,falsefor the three endings. - Convert it to a signal with
toSignal(..., { initialValue: false }). - In the template,
@if (loading()) { <progress /> }.
Why handle all three endings? If you only hide on NavigationEnd, a cancelled or failed navigation leaves the spinner forever.
Variations:
- Add a small delay before showing the indicator (about 150 ms), so fast navigations do not flash.
- Use the
withNavigationErrorHandlerrouter feature to redirect to an error page. - For a global progress bar, put this logic in one root component, not in each page.
- Add
aria-busyon the main region and an accessible label on the indicator.
Remember that router.events only emits while someone subscribes, so use it in a component or service created at start-up. In SSR, events also fire on the server.
typescript
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router, RouterOutlet } from '@angular/router';
import { filter, map } from 'rxjs';
@Component({
selector: 'app-shell',
imports: [RouterOutlet],
template: `
@if (loading()) {
<progress aria-label="Loading page"></progress>
}
<main [attr.aria-busy]="loading()"><router-outlet /></main>
`,
})
export class ShellComponent {
private readonly router = inject(Router);
readonly loading = toSignal(
this.router.events.pipe(
filter(
(e) => e instanceof NavigationStart || e instanceof NavigationEnd || e instanceof NavigationCancel || e instanceof NavigationError,
),
map((e) => e instanceof NavigationStart),
),
{ initialValue: false },
);
}Angular 20.3, strict template check passes. A Karma test checks that the signal is true during a navigation and false after it ends.
Key points
- `router.events` emits navigation events. Start begins, and End, Cancel or Error finish.
- Handle all three ending events or the spinner can stay on forever.
- `toSignal` gives a simple signal for the template.
- Delay showing the indicator to avoid flashes. Add accessible labels.
Common mistakes
- Only handling `NavigationEnd`, so cancelled navigations leave the spinner on.
- Subscribing in every page component instead of once at the root.
- Forgetting to unsubscribe in older code that subscribes manually.
Follow-ups
- When is `NavigationCancel` emitted?
- How would you delay showing the spinner by 150 ms?
- How would you handle a lazy chunk that fails to load?
Tips
- List the three endings. It is the usual trap.
- Use `toSignal` to show modern style.
- Mention accessibility.
Evaluation rubric
- Names the navigation events correctly.
- Handles all ending events.
- Implements the indicator cleanly.