Angular2 NavigationEnd 事件的“事件"类型不存在属性“url" [英] Property 'url' does not exist on type 'Event' for Angular2 NavigationEnd Event

查看:29
本文介绍了Angular2 NavigationEnd 事件的“事件"类型不存在属性“url"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Typescript doesn't recognize the properties of the type Event that is built into the Angular Router. Is there something on tsd that I can use to solve this? Event is the super class of classes NaviagationEnd, NavigationStart

推荐答案

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

You have to import { Event } from @angular/router;. Try moving console into the if block with condition.

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

这篇关于Angular2 NavigationEnd 事件的“事件"类型不存在属性“url"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆