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

Issue

    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

Solution

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

Answered By – Arpit Agarwal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published