首页 > 解决方案 > 以角度 6 在路由中传递两个参数

问题描述

我正在通过路由传递两个参数,但它不起作用。当我点击 routerLink 时,它会将我重定向到我的默认路由。
表示没有接受参数的路由。
我错过了什么..?
提前致谢。

路径是:

{path:'Buy/:term1/:term2', component: Buycomponent}

我正在传递这样的参数:

<a [routerLink] ="['/Buy',{term1:'sale',term2:'home'}]">data</a>

在组件中我得到这样的参数:

this.route.params.subscribe(response => console.log(response.term1 , responce.term2));  

.
更新...

对于单一值,它工作正常。

{path:'Buy/:term1, component: Buycomponent}

并像这样传递参数:

<a [routerLink] ="['/Buy','sale']">data</a>

在组件中我得到这样的参数:

this.route.params.subscribe(response => console.log(response.term1));

标签: angular

解决方案


尝试这个:

<a [routerLink] ="['/Buy','sale','home']">data</a>

当您想要传递可选参数时使用此语法

<a [routerLink] ="['/Buy',{term1:'sale',term2:'home'}]">data</a>

推荐阅读