BathgateIO
BathgateIO

Reputation: 302

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. Is there something on tsd that I can use to solve this? Event is the super class of classes NaviagationEnd, NavigationStart

Upvotes: 11

Views: 16861

Answers (2)

Amir Shaikh
Amir Shaikh

Reputation: 207

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.

step 1: add import statement

import { Router, NavigationStart, Event as NavigationEvent } from '@angular/router';

` step 2: add below code.

this.router.events
  .subscribe(
    (event: NavigationEvent) => {
      if(event instanceof NavigationEnd) {
        console.log(event.url);
      }
    });

Upvotes: 0

Arpit Agarwal
Arpit Agarwal

Reputation: 4023

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

Upvotes: 24

Related Questions