Angular2 - 如何使用带动态网址的路由器

Pat*_*t M 6 angular

假设我有这个嵌套的itemListComponent,它是rootComponent的子节点,使用以下模板:

<span *ngFor="#item of list">
    <a [routerLink]="[item.url]">{{item.title}}</a> |
</span>
Run Code Online (Sandbox Code Playgroud)

从Json服务提供的路径item.url可以具有以下路径结构之一:

  • / category1 /(内容id)/(用短划线( - )连接的标题)
  • / category2 /(内容id)/(用短划线( - )连接的标题)
  • / category3 /(内容id)/(用短划线( - )连接的标题)

例子:

category2/987654/hello-world
category2/123456/hi-teacher
category3/554433/yo-brother
Run Code Online (Sandbox Code Playgroud)

如何实现@RouteConfig以便这些链接使用itemDetail组件?

对不起,如果这看起来不是很详细.我没有找到一个明确的如何引导路由由具有完整路径的服务(未组装的路径)提供的URL

小智 14

试试这个:

<span *ngFor="#item of list">
   <a [routerLink]="['/category', item.url]">
        {{item.title}}
   </a>
</span>
Run Code Online (Sandbox Code Playgroud)

routes.ts:

{path: "category/:id", component: CategoryComponent}
Run Code Online (Sandbox Code Playgroud)

  • 这就是问题的答案.可能是角度团队最近补充了这一点.谢谢! (2认同)