首页 > 解决方案 > 如何在 Angular 2/5 中从 RouterLinkWithHref 中找到匹配的路由?

问题描述

我正在尝试使用指令隐藏/删除菜单/子菜单项。

我正在使用@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']} },
...
];

在此处输入图像描述

标签: angularangular5angular2-directivesangular2-router

解决方案


推荐阅读