Assume we start navigation in once component but want to know where we go in another (e.g. in a menu).
this.router.events.subscribe(r => {
if (r instanceof NavigationEnd) {
const tree: UrlTree = this.router.parseUrl(r.url);
const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
const s: UrlSegment[] = g.segments;
if (s.length > 0)
this.activePath = s[0].path;
}
});
ActivatedRoute fires only once, while listening to the router events is effective in figuring out current navigation path.
Tags
Angular