Angular 2 @ angular/router 3.0.0-alpha.7-访问多个参数 [英] Angular 2 @angular/router 3.0.0-alpha.7 - Accessing Multiple Parameters
问题描述
我正在尝试最近发布的Angular 2中的新路由器,即Angular 2 @ angular/router 3.0.0-alpha.7
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?
我的意思是我们如何从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
});
另请参阅此处的HeroDetailComponent组件 http://plnkr.co/edit/JtuOAZsZPhkn1CISQaO9?p=preview
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
});
柱塞示例 (HeroDetailComponent)
Plunker sample (HeroDetailComponent)
这篇关于Angular 2 @ angular/router 3.0.0-alpha.7-访问多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!