javascript – Angular 2 Routes – 是否可以使用ngFor从路由创建动态routerLinks?

我想在具有已定义路径的特定模块中获取所有路由,并使用ngFor循环遍历它们并在我的组件html中创建动态链接列表.

ngFor示例(overview.component.html):

<li *ngFor="let item of items; let i = index;">
   <a routerLink="/{route.path}">{route.path}</a>
</li>

模块示例(base.module.ts):

...

const routerConfig = [
  {
    path: '',
    component: BaseComponent,
    children: [
      {
        path: '',
        component: OverviewComponent
      },
      {
        path: 'group-one',
        component: OverviewComponent
      },
      {
        path: 'group-one/:id',
        component: DetailComponent
      },
      {
        path: 'group-two',
        component: OverviewComponent
      },
      {
        path: 'group-two/:id',
        component: DetailComponent
      }
    ]
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(routerConfig)
  ]
  ...
})
export class BaseModule { }

在尝试使用ActivatedRoute,Router等进行各种实验后,我无法找到可供使用此信息的对象.

目前这可以通过Angular 2路由器吗?

我怎么能解决这个问题呢?

最佳答案 这段代码

<a routerLink="/{route.path}">

传递/{route.path}字面上作为路径路径

你可能要么

<a [routerLink]="'/' + route.path">

要么

<a routerLink="/{{route.path}}">

对于Angular来评估表达式,绑定需要只有[]或{{}}中的一个(从不同时)

[propName]="..."

要么

propName="{{...}}"

后者总是将结果字符串化,因此不适合传递对象.

点赞