Irshad Ali
Irshad Ali

Reputation: 1213

How to find matched Route from RouterLinkWithHref in Angular 2/5?

I am trying to hide/remove menu/submenu items using directive.

I am reading RouterLinkWithHref using @ContentChild but could not find Route. I am interested in reading data object that is provided in route.

Below is some code to make more sense. Let me know, if something could be useful to answer or dig deeper.

@Directive({
  selector: '[roleCheck]'
})
export class RoleCheckDirective implements AfterViewInit {

  @ContentChild(RouterLinkWithHref) routerLinkWithHref: RouterLinkWithHref;

  constructor(private elementRef: ElementRef) { }

  ngAfterViewInit() {
    console.log(this.routerLinkWithHref);

    // do something here to find matched route, and read route.data for allowed roles

  }
}

Below home route has data containing roles. Angular find the matched route some how from RouterLink and RouterLinkWithHref. Angular also provide RouterLinkActive that must read RouterLink/RouterLinkWithHref to show active route in navigation menu. So, the question is how, which methods are available. how i can see their implementation. I am using angular with typescript. I have seen the typings of RouterLinkActive. Any help is appreciated.

const routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent, data: { roles: ['Admin', 'Analyst']} },
...
];

enter image description here

Upvotes: 1

Views: 1179

Answers (0)

Related Questions