Reputation: 1203
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!
Upvotes: 28
Views: 71814
Reputation: 86740
easiest way to find current path is either you directly use
this.router.url
or you can check current path on every router change using its events like this
this.router.events.subscribe((res) => {
console.log(this.router.url,"Current URL");
})
where router
here is instance created in constructor like this
constructor(private router: Router){ ... }
Upvotes: 33
Reputation: 1203
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;
}
});
Upvotes: 39
Reputation: 5391
constructor(private route:ActivatedRoute) {
console.log(route);
}
or
constructor(private router:Router) {
router.events.subscribe(...);
}
Upvotes: 4