路由器事件

时间:2016-12-27 14:13:13

标签: angular typescript

我有以下代码:

 this.routerSubscription = this._router.events
     .filter(event => event instanceof NavigationStart)
     .subscribe((event:Event) => {
        ....
     });

这引起了我的错误:

error TS2345: Argument of type '(event: Event) => void' is not assignable to parameter of type 'NextObserver<NavigationStart | NavigationEnd | NavigationCancel | NavigationError> | ErrorObserve...'.
  Type '(event: Event) => void' is not assignable to type '(value: NavigationStart | NavigationEnd | NavigationCancel | NavigationError) => void'.
    Types of parameters 'event' and 'value' are incompatible.
      Type 'NavigationStart | NavigationEnd | NavigationCancel | NavigationError' is not assignable to type 'Event'.
        Type 'NavigationStart' is not assignable to type 'Event'.
          Property 'bubbles' is missing in type 'NavigationStart'.

我做错了什么?

1 个答案:

答案 0 :(得分:10)

我猜你忘记了:

import { Event } from '@angular/router';

或者使用它:

import { Event as NavigationEvent } from '@angular/router';
...
.subscribe((event: NavigationEvent)

另见