angular监听路由

// 声明订阅对象
routerChange: Subscription;

// 发生变化是才会触发,初始化时不会触发
ngOnInit() {
  this.routerChange = this.router.events.subscribe((event) => {
    if (event instanceof NavigationEnd) {
      this.showAnchor = event.url.includes('resume');
    }
  });
  this.showAnchor = this.route.children[0].routeConfig.path === 'resume';
}


ngOnDestroy(): void {
  if (this.routerChange) {
    this.routerChange.unsubscribe();
  }
}

 

posted @ 2020-09-08 17:49  litiyi  阅读(419)  评论(0编辑  收藏  举报