I have a route with route children like this:
{
path: \'dashboard\',
children: [{
path: \'\',
canActivate: [CanActivateAuthGuard],
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
);
}