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

Bat*_*eIO 6 routing router angularjs angular-routing angular

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

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

Arp*_*wal 9

你必须import { Event } from @angular/router;.尝试将控制台移动到if具有条件的块中.

this.subscription = this.router.events.subscribe((event:Event) => {
  if(event instanceof NavigationEnd ){
    console.log(event.url);
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 不要认为您需要导入 Event 并显式定义它;“NavigationEnd 事件实例”位应该足够了。 (2认同)