简体   繁体   中英

Angular routerLink to path

I need to go to localhost:4200/route/SOME_PARAMETER where SOME_PARAMETER is a property of the component.

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)

and the routerLink would look like that

[routerLink]="'route/' + parameter" (while "parameter" would be a global variable of the component.

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.

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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