How to detect a route change in Angular?

前端 未结 22 1548
花落未央
花落未央 2020-11-22 04:40

I am looking to detect a route change in my AppComponent.

Thereafter I will check the global user token to see if he is logged in. Then I can redirect t

22条回答
  •  孤独总比滥情好
    2020-11-22 05:09

    Angular 7, if you want to subscribe to router

    import { Router, NavigationEnd } from '@angular/router';
    
    import { filter } from 'rxjs/operators';
    
    constructor(
      private router: Router
    ) {
      router.events.pipe(
        filter(event => event instanceof NavigationEnd)  
      ).subscribe((event: NavigationEnd) => {
        console.log(event.url);
      });
    }
    

提交回复
热议问题