I am just trying the new router in Angular 2 announced recently i.e. Angular 2 @angular/router 3.0.0-alpha.7
I know that in new router we can access route parameters using below code:
this.activatedRoute.params
.map(params => params['id'])
.subscribe((id) => {
//use param id
});
Can anyone please guide how do we handle the case in which we have multiple multiple parameters in our route?
I mean how do we retrieve the values of multiple parameters from route.
One way you could do it is using Parameter handling and Destructuring like this:
this.activatedRoute.params
.map(params => [params['id'], params['p2'], params['p3']]) <== pass desired array
.subscribe(([id, p2, p3]) => { <== destructuring params
//use param id, p2 or p3
});
See also HeroDetailComponent component here http://plnkr.co/edit/JtuOAZsZPhkn1CISQaO9?p=preview
Or you can write a bit simple:
this.activatedRoute.params
.subscribe(({id, p2, p3}) => { <== destructuring params
//use param id, p2 or p3
});
Plunker sample (HeroDetailComponent)
来源:https://stackoverflow.com/questions/37903073/angular-2-angular-router-3-0-0-alpha-7-accessing-multiple-parameters