使用 routerLink 导航时,角度路由器事件不会触发



>我有一个组件,我需要使用以下代码检查当前的路由网址:

ngOnInit() {
    this.router.events.subscribe((val: any) => {
        if (val instanceof NavigationStart) {
        }
    });
}

出于某种原因,当我使用 [routerLink]="['/person/edit', perosn.id] 导航到该组件时,如果我引用页面,则在单击链接进行首次事件时不会触发路由器事件。然后,如果我再次单击该链接,则会触发事件。

有什么想法为什么一开始不会被解雇吗?

ngOnInit(( 仅在组件实例化后调用一次,但不在路由更改时调用。

您可以注入路由器并订阅其事件或参数,以获取有关路由更改的通知。

constructor(private route: ActivatedRoute) { }
ngOnInit() {
     this.route.params.subscribe(val: any) => {
        if (val instanceof NavigationStart) {
        }
    });
}

最新更新