How to detect a route change in Angular?

前端 未结 22 2008
花落未央
花落未央 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:17

    I would write something like this:

    ngOnInit() {
    this.routed = this.router.events.map( event => event instanceof NavigationStart )
      .subscribe(() => {
      } );
    }
    
    ngOnDestroy() {
    this.routed.unsubscribe();
    }
    

提交回复
热议问题