Angular 2 @angular/router 3.0.0-alpha.7 - 访问多个参数

2024-03-16

我只是在尝试最近发布的 Angular 2 中的新路由器,即 Angular 2 @angular/router 3.0.0-alpha.7

我知道在新路由器中我们可以使用以下代码访问路由参数:

this.activatedRoute.params
            .map(params => params['id'])
            .subscribe((id) => {

             //use param id

             });

谁能指导我们如何处理路线中有多个参数的情况?

我的意思是我们如何从路由中检索多个参数的值。


你可以做到这一点的一种方法是使用参数处理 http://exploringjs.com/es6/ch_parameter-handling.html and 解构 http://exploringjs.com/es6/ch_destructuring.html像这样:

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 http://plnkr.co/edit/JtuOAZsZPhkn1CISQaO9?p=preview

或者你可以写得简单一些:

this.activatedRoute.params
  .subscribe(({id, p2, p3}) => { <== destructuring params
      //use param id, p2 or p3
   });

普朗克样本 http://plnkr.co/edit/3AAwxHUQ6JFdOFHg0OMq?p=preview(英雄细节组件)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2 @angular/router 3.0.0-alpha.7 - 访问多个参数 的相关文章

随机推荐