skills/dev-skills/angular-developer/references/router-lifecycle.md
Angular Router emits events through the Router.events observable, allowing you to track the navigation lifecycle from start to finish.
NavigationStart: Navigation begins.RoutesRecognized: Router matches the URL to a route.GuardsCheckStart / End: Evaluation of canActivate, canMatch, etc.ResolveStart / End: Data resolution phase (fetching data via resolvers).NavigationEnd: Navigation completed successfully.NavigationCancel: Navigation canceled (e.g., guard returned false).NavigationError: Navigation failed (e.g., error in resolver).Inject the Router and filter the events observable.
import {Router, NavigationStart, NavigationEnd} from '@angular/router';
export class MyService {
private router = inject(Router);
constructor() {
this.router.events.pipe(filter((e) => e instanceof NavigationEnd)).subscribe((event) => {
console.log('Navigated to:', event.url);
});
}
}
Enable detailed console logging of all routing events during application bootstrap.
provideRouter(routes, withDebugTracing());
NavigationStart fires and hide it on NavigationEnd/Cancel/Error.NavigationEnd.Scroll events for custom scroll behavior.