【问题标题】:How do I effectively add a parameter to my Angular 7 routing?如何有效地将参数添加到我的 Angular 7 路由?
【发布时间】:2019-04-04 14:17:47
【问题描述】:

所以我尝试在我的电子商务中添加一个简单的详细信息部分。两条路径如下所示:

{ path: 'items', component: ItemListComponent},
{ path: 'items/details/:id', component: ItemDetailComponent},

这是我的来自 ItemList 组件模板的 routerlink:

<a routerLink="['/items/details',item.id]">See Details</a>

但是当我点击按钮时,我的 URL 不是 /items/details/{id}。复制链接地址给我这个:/items/%5B'/items/details',item.id%5D

我的方法有什么问题,我该如何解决?我只想让我的按钮从项目转到项目/详细信息/{id}。我的 item.id 确实包含正确的值,所以不是这样。

我也得到了错误:

Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'items/%5B'/items/details',item.id%5D'

错误:无法匹配任何路由。 URL 段:'items/%5B'/items/details',item.id%5D'

【问题讨论】:

    标签: angular angular-routing


    【解决方案1】:

    您需要将 routerLink 括在方括号中:

    <a [routerLink]="['/items/details',item.id]">See Details</a>
    

    【讨论】:

    • 不用担心@PardonMyCheckmate
    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2019-07-12
    • 1970-01-01
    • 2011-09-17
    • 2019-10-26
    相关资源
    最近更新 更多