Sub topic 1
How the router navigates: a short lifecycle tour
Prerequisites
This chapter assumes you know basic TypeScript, Angular components and services, and the routing basics: a Routes array, <router-outlet>, routerLink, and route parameters. It also assumes you can read RxJS Observables and Promises, and that you understand dependency injection. Later chapters build guards and resolvers on top of the lifecycle described here.
What a navigation is
A navigation starts when something asks the router to go to a URL. That can be a routerLink click, a call to router.navigate(), or the browser's back button. The router then works through a fixed sequence of steps. Guards and resolvers are hooks into that sequence.
The main phases
- Match. The router compares the URL to your
Routesarray and builds a tree of the routes that match. Lazy-loaded routes are loaded here if they are needed. - Redirect. Any
redirectToentries are applied, and the router settles on the final URL. - Guards. The router asks the guards on the route whether the navigation may continue. This is where you can cancel it or send the user elsewhere.
- Resolvers. The router runs the resolvers and waits for the data they return.
- Activate. The router creates the components, places them in the outlets, and updates the browser URL.
Where the URL changes
The browser URL changes late, in the activate step, not when the navigation starts. If a guard blocks the navigation, the address bar stays on the current page. A guard that returns a redirect starts a new navigation. The router does not complete the first one.
Observing the lifecycle
The router publishes events for each phase. You can log them to see the order for yourself.
import { Component, inject } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
template: '',
})
export class AppComponent {
constructor() {
inject(Router).events.subscribe((event) => {
if (
event instanceof NavigationStart ||
event instanceof NavigationEnd ||
event instanceof NavigationCancel ||
event instanceof NavigationError
) {
console.log(event.constructor.name, event.url);
}
});
}
}A successful navigation logs NavigationStart and then NavigationEnd. A navigation stopped by a guard logs NavigationCancel instead of NavigationEnd. A thrown error logs NavigationError.
Why this matters
Because guards and resolvers run before activation, the user keeps seeing the old page while they work. Slow guards or resolvers therefore make navigation feel slow, and you need to plan for that. The following chapters cover how to write each hook and how to handle that delay.
Exercise
A user clicks a link to a page protected by a guard that returns false. Which navigation event is emitted after NavigationStart, and does the address bar change?
Show answer
NavigationCancel is emitted, not NavigationEnd. The address bar does not change, because the URL is updated only during activation, which never happens.