Angular2 NavigationEnd 事件的“事件”类型中不存在属性“url”

2023-11-24

    this.subscription = this.router.events.subscribe((event:Event) => {
        console.log(event.url); ##### Error : Property 'url' does not exist on type 'Event'.
   }

Typescript 无法识别 Angular Router 中内置的事件类型的属性。 tsd 上有什么可以用来解决这个问题吗? Event 是 NaviagationEnd、NavigationStart 类的超类


你必须import { Event } from @angular/router;。尝试将控制台移至if有条件地阻止。

this.subscription = this.router.events.subscribe((event:Event) => {
  if(event instanceof NavigationEnd ){
    console.log(event.url);
  }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular2 NavigationEnd 事件的“事件”类型中不存在属性“url” 的相关文章

随机推荐