我试图使用指令隐藏/删除菜单/子菜单项 .
我正在使用@ContentChild读取RouterLinkWithHref,但找不到Route . 我有兴趣阅读路线中提供的数据对象 .
下面是一些更有意义的代码 . 让我知道,如果有什么东西可以回答或深入挖掘 .
@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
}
}
家庭路线下面有包含角色的数据 . Angular从RouterLink和RouterLinkWithHref中找到匹配的路由 . Angular还提供RouterLinkActive,必须读取RouterLink / RouterLinkWithHref以在导航菜单中显示活动路由 . 所以,问题是如何,哪些方法可用 . 我怎么能看到他们的实施 . 我正在使用带有打字稿的角度 . 我已经看到了RouterLinkActive的类型 . 任何帮助表示赞赏 .
const routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, data: { roles: ['Admin', 'Analyst']} },
...
];