What is the easiest way to get Current Route Path Name in Angular?

jaruesink picture jaruesink · Apr 12, 2017 · Viewed 54.9k times · Source

I was looking for a good way to get the current route's path name. This was the easiest I could find.

this.route.snapshot.firstChild.url[0].path

Is there a better way? Thanks!

Answer

jaruesink picture jaruesink · Apr 12, 2017

Thanks everyone for the answers. Here is what I found that I had to do.

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