InterviewHubPRO
Section Overview
Q70

Question 70 of 283

Part 3: Angular Router, Navigation & SSR Universal (63 - 75)

25%
Angular & Signals (283 Qs)•Part 3: Angular Router, Navigation & SSR Universal (63 - 75)

What are router events?

During each navigation, the Router emits navigation events through the Router.events property allowing you to track the lifecycle of the route.

The sequence of router events is as below,

  1. NavigationStart,
  2. RouteConfigLoadStart,
  3. RouteConfigLoadEnd,
  4. RoutesRecognized,
  5. GuardsCheckStart,
  6. ChildActivationStart,
  7. ActivationStart,
  8. GuardsCheckEnd,
  9. ResolveStart,
  10. ResolveEnd,
  11. ActivationEnd
  12. ChildActivationEnd
  13. NavigationEnd,
  14. NavigationCancel,
  15. NavigationError
  16. Scroll

Practical Code Example

typescript
// Listening to Router Events:
import { Component, inject } from '@angular/core';
import { Router, NavigationStart, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';

@Component({ selector: 'app-nav', standalone: true, template: '' })
export class NavComponent {
  private router = inject(Router);

  constructor() {
    this.router.events
      .pipe(filter(e => e instanceof NavigationEnd))
      .subscribe((e: NavigationEnd) => console.log('Navigated to:', e.urlAfterRedirects));
  }
}