问题
I know I can pass a parameter to routerLink
for routes such as
/user/:id
by writing
[routerLink]="['/user', user.id]"
but what about routes such as this one:
/user/:id/details
Is there a way to set this parameter or should I consider a different URL scheme?
回答1:
In your particular example you'd do the following routerLink
:
[routerLink]="['user', user.id, 'details']"
To do so in a controller, you can inject Router
and use:
router.navigate(['user', user.id, 'details']);
More info in the Angular docs Link Parameters Array section of Routing & Navigation
回答2:
Maybe it is really late answer but if you want to navigate another page with param you can,
[routerLink]="['/user', user.id, 'details']"
also you shouldn't forget about routing config like ,
[path: 'user/:id/details', component:userComponent, pathMatch: 'full']
回答3:
First configure in table:
const routes: Routes = [
{
path: 'class/:id/enrollment/:guid',
component: ClassEnrollmentComponent
}
];
now in type script code:
this.router.navigate([`class/${classId}/enrollment/${4545455}`]);
receive params in another component
this.activatedRoute.params.subscribe(params => {
let id = params['id'];
let guid = params['guid'];
console.log(`${id},${guid}`);
});
来源:https://stackoverflow.com/questions/38062702/how-to-pass-a-parameter-to-routerlink-that-is-somewhere-inside-the-url