Angular 2 Router Link with multiple parameters
10,009
I had the same issue for this kind of route
you should use routerLink
like this:
<a routerLink="/projects/{{progectId}}/settings" routerLinkActive="active">cool link</a>
and change your route path
in routing module
like this:
{ path: ':id/settings', component: YourComponent}
for additional information to get projectId
you should follow these step:
- Inject
ActivatedRoute
object
in yourcomponent constructor
. - Define a
variable
calledprojectId
in yourcomponent
. - Get
params
byactivatedRoute
object
inngOnInit()
method. Finally you should
get
projectId
in yourhtml
like this:{{projectId}}
import {Router, ActivatedRoute, Params} from '@angular/router'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent implements OnInit { private projectId: string; constructor(private activatedRoute: ActivatedRoute) {} ngOnInit() { this.activatedRoute.params.subscribe((params: Params) => { this.projectId = params['id']; }); }}
Author by
Roman Skydan
Hi. I'm ui developer. Have experience with different js framework and design.
Updated on June 15, 2022Comments
-
Roman Skydan almost 2 years
I have a route like this
path: 'projects/:id/settings'
And in my header.component.html I want to have a link to this page
<a routerLink="['projects', progectId,'settings']" routerLinkActive="active">cool link</a>
I have a project.component, where when I click on some project I go on the project page. And then I need have a possibility go to
projects/:id/settings
or another similar route.How can I pass
progectId
variable from projects.component?
Or maybe somebody knows another way to implement this.