Angular 如何仅为第一次导航更改获取路由器导航结束url

Angular 如何仅为第一次导航更改获取路由器导航结束url,angular,Angular,这将为所有路由更改添加要运行的代码。我要换第一条路线。我试过这样的东西。它起作用了,但不知道它会产生怎样的影响 this.router.events.subscribe( event => { if (event instanceof NavigationEnd) console.log(event.urlAfterRdirects); } ); 没有一种方法可以做到这一点,您的解决方案会很好地工作Smokey Dawson:订阅中的取消订阅是如何工作的?使用这种方法有风

这将为所有路由更改添加要运行的代码。我要换第一条路线。我试过这样的东西。它起作用了,但不知道它会产生怎样的影响

this.router.events.subscribe(
  event => {
    if (event instanceof NavigationEnd) console.log(event.urlAfterRdirects);
  }
);

没有一种方法可以做到这一点,您的解决方案会很好地工作Smokey Dawson:订阅中的取消订阅是如何工作的?使用这种方法有风险吗?
this.myEvents = this.router.events.subscribe(
  event => {
    if (event instanceof NavigationEnd) {
          console.log(event.urlAfterRdirects);
           this.myEvents.unsubscribe();}

  }
);