Oddleif
Oddleif

Reputation: 791

Angular get route path - not resolved url

Is there a way in Angular to get the actual route in the NavigationEnd event instead of just the resolved url which event.url returns? I don't what the resolved params.

export class AppComponent {
  constructor(private router: Router) {
    this.router.events
    .filter(event => event instanceof NavigationEnd)
    .subscribe((event: NavigationEnd) => {
        // how to get route path? Such as 'someList/:id'
    });
  }
}

Upvotes: 3

Views: 997

Answers (2)

JayChase
JayChase

Reputation: 11525

I don't know if this is the best way but

this.router.routerState.snapshot.root.firstChild.routeConfig

will get you the route config for the current route which has the configured path,

Upvotes: 3

Mike Tung
Mike Tung

Reputation: 4821

You want something like this?

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

Upvotes: -1

Related Questions