Angular routerLink 附加路由而不是替换
Angular routerLink appends route instead of replacing
我的 Angular 应用程序中有以下路由配置
...
{ path: 'item/new', component: ItemDetailsComponent, pathMatch: 'full', data: { new: true } },
{ path: 'item/:id/:action', component: ItemDetailsComponent, pathMatch: 'full' },
...
were :action 可以是“查看”或“编辑”。当我将它们直接输入浏览器时,所有三种可能的路径都有效。但是,项目模板中的路由器 links 不会:
<div routerLink="['/item', id, 'edit']">Test</div>
“id”是在组件中设置的,所以这可能不是问题所在。浏览器控制台错误表明 url 以某种方式附加:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'item/2/view/%5B'/item',%20id,%20'edit'%5D'
Error: Cannot match any routes. URL Segment: 'item/2/view/%5B'/item',%20id,%20'edit'%5D'
由于路由器 link 确实以斜杠开头,我希望它能工作。
你应该使用
<div [routerLink]="['/item', id, 'edit']">Test</div>
或
<div routerLink="/item/{{id}}/edit">Test</div>
我的 Angular 应用程序中有以下路由配置
...
{ path: 'item/new', component: ItemDetailsComponent, pathMatch: 'full', data: { new: true } },
{ path: 'item/:id/:action', component: ItemDetailsComponent, pathMatch: 'full' },
...
were :action 可以是“查看”或“编辑”。当我将它们直接输入浏览器时,所有三种可能的路径都有效。但是,项目模板中的路由器 links 不会:
<div routerLink="['/item', id, 'edit']">Test</div>
“id”是在组件中设置的,所以这可能不是问题所在。浏览器控制台错误表明 url 以某种方式附加:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'item/2/view/%5B'/item',%20id,%20'edit'%5D'
Error: Cannot match any routes. URL Segment: 'item/2/view/%5B'/item',%20id,%20'edit'%5D'
由于路由器 link 确实以斜杠开头,我希望它能工作。
你应该使用
<div [routerLink]="['/item', id, 'edit']">Test</div>
或
<div routerLink="/item/{{id}}/edit">Test</div>