Angular 2 @angular/router 3.0.0-alpha.7 - Accessing Multiple Parameters

本秂侑毒 提交于 2019-12-10 10:48:29

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!