Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Router event

I have the following piece of code:

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

Which throws me an error of:

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'.

What am I doing wrong?

like image 799
uksz Avatar asked Dec 27 '16 14:12

uksz


1 Answers

I guess you forgot about:

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

Or use it like:

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

See also

  • How to detect a route change in Angular 2?
like image 111
yurzui Avatar answered Nov 01 '22 07:11

yurzui