简体   繁体   English

角度路由器链接到路径

[英]Angular routerLink to path

I need to go to localhost:4200/route/SOME_PARAMETER where SOME_PARAMETER is a property of the component.我需要转到 localhost:4200/route/SOME_PARAMETER,其中 SOME_PARAMETER 是组件的属性。

Currently I have this:目前我有这个:

<a [routerLink]="['/route/${SOME_PARAMETER}']">

But it's taking me to an incorrect route.但它把我带到了一条错误的路线上。

I think I just need to figure out the syntax.我想我只需要弄清楚语法。

Could you also provide your routing module?你能提供你的路由模块吗?

Anyways,无论如何,

in the routing module you would have following syntax:在路由模块中,您将具有以下语法:

{ path: 'route/:id', component: SomeComponent} (with or without "route", you got to know) { path: 'route/:id', component: SomeComponent} (带不带“路由”,你懂的)

and the routerLink would look like that和 routerLink 看起来像那样

[routerLink]="'route/' + parameter" (while "parameter" would be a global variable of the component. [routerLink]="'route/' + parameter" (而“参数”将是组件的全局变量。

What you're looking for is adding Dynamic Values to a routerLink.The syntax is the following, you can pass as many arguments as you want.您正在寻找的是向 routerLink 添加动态值。语法如下,您可以根据需要传递任意数量的参数。

<a [routerLink]="['route',parameter]">

Of course you need to set up your routing module correctly.当然,您需要正确设置路由模块。 Considering your example it would be:考虑到您的示例,它将是:

[{ path: 'route/:some_param', component: ComponentToBeRendered }]

From the official documentation:来自官方文档:

You can use dynamic values to generate the link.您可以使用动态值来生成链接。 For a dynamic link, pass an array of path segments, followed by the params for each segment.对于动态链接,传递一组路径段,然后是每个段的参数。 For example, ['/team', teamId, 'user', userName, {details: true}] generates a link to /team/11/user/bob;details=true.例如,['/team', teamId, 'user', userName, {details: true}] 生成指向 /team/11/user/bob;details=true 的链接。

https://angular.io/api/router/RouterLink https://angular.io/api/router/RouterLink

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM