How to detect a route change in Angular?

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

    @Ludohen answer is great, but in case you don't want to use instanceof use the following

    this.router.events.subscribe(event => {
      if(event.constructor.name === "NavigationStart") {
        // do something...
      }
    });
    

    with this way you can check the current event name as a string and if the event occurred you can do what you planned your function to do.

提交回复
热议问题