[英]Property 'url' does not exist on type 'Event' for Angular2 NavigationEnd Event
this.subscription = this.router.events.subscribe((event:Event) => {
console.log(event.url); ##### Error : Property 'url' does not exist on type 'Event'.
}
Typescript doesn't recognize the properties of the type Event that is built into the Angular Router. Typescript 无法识别 Angular 路由器中内置的 Event 类型的属性。 Is there something on tsd that I can use to solve this?
tsd 上有什么东西可以用来解决这个问题吗? Event is the super class of classes NaviagationEnd, NavigationStart
Event 是类 NaviagationEnd、NavigationStart 的超类
You have to import { Event } from @angular/router;
您必须
import { Event } from @angular/router;
. . Try moving console into the
if
block with condition.尝试将控制台移动到带有条件的
if
块中。
this.subscription = this.router.events.subscribe((event:Event) => {
if(event instanceof NavigationEnd ){
console.log(event.url);
}
});
Router event are usually used to watch router states and can write custom logic as requirement.路由器事件通常用于观察路由器状态,可以根据需要编写自定义逻辑。
we can use NavigationStart or NavigationEnd to achieve states.我们可以使用 NavigationStart 或 NavigationEnd 来实现状态。
step 1: add import statement第 1 步:添加导入语句
import { Router, NavigationStart, Event as NavigationEvent } from '@angular/router';
` step 2: add below code. ` 第 2 步:添加以下代码。
this.router.events
.subscribe(
(event: NavigationEvent) => {
if(event instanceof NavigationEnd) {
console.log(event.url);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.