Angular 2 Get Parent Activated Route


Answer :

You can do this by using the parent property on the ActivatedRoute - something like this.

export class MyComponent implement OnInit {      constructor(private activatedRoute: ActivatedRoute) {}      ngOnInit() {         this.activatedRoute.parent.url.subscribe((urlPath) => {             const url = urlPath[urlPath.length - 1].path;         })     }  } 

You can see everything from the ActivatedRoute in more detail here: https://angular.io/api/router/ActivatedRoute


You can check for parent route by determining if only one slash is present in it:

 constructor(private router: Router) {}   ngOnInit() {       this.router.events.pipe(filter(e => e instanceof NavigationEnd)).subscribe((x: any) => {           if (this.isParentComponentRoute(x.url)) {             // logic if parent main/parent route           }         });   }   isParentComponentRoute(url: string): boolean {     return (       url         .split('')         .reduce((acc: number, curr: string) => (curr.indexOf('/') > -1 ? acc + 1 : acc), 0) === 1     );   } 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?