【问题标题】:Property 'url' does not exist on type 'Event' for Angular2 NavigationEnd EventAngular2 NavigationEnd 事件的“事件”类型上不存在属性“url”
【发布时间】:2016-11-18 19:12:50
【问题描述】:
    this.subscription = this.router.events.subscribe((event:Event) => {
        console.log(event.url); ##### Error : Property 'url' does not exist on type 'Event'.
   }

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

【问题讨论】:

  • 你有没有从@angular/router 导入事件。尝试使用条件 if(event instanceof NavigationEnd) 在 if 块中移动控制台
  • 你明白了!谢谢,我是这个打字稿的新手
  • 将此更新为答案。

标签: angularjs angular routing router angular-routing


【解决方案1】:

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

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

【讨论】:

  • 谢谢,这就是我在寻找过滤特定事件的方法。
  • 不要认为需要导入 Event 并明确定义; event instanceof NavigationEnd 位就足够了。
猜你喜欢
  • 2017-08-15
  • 2017-04-27
  • 1970-01-01
  • 2017-07-16
  • 2021-06-04
  • 2019-11-05
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多