Property 'url' does not exist on type 'Event' for Angular2 NavigationEnd Event

BathgateIO picture BathgateIO · Jul 15, 2016 · Viewed 9.8k times · Source
    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. Is there something on tsd that I can use to solve this? Event is the super class of classes NaviagationEnd, NavigationStart

Answer

Arpit Agarwal picture Arpit Agarwal · Jul 19, 2016

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);
  }
});