javascript – 如何在[routerLink] angular 2中传递路径参数

前端之家收集整理的这篇文章主要介绍了javascript – 如何在[routerLink] angular 2中传递路径参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用角度2创建一个应用程序,并且想要传递params来标记[routerLink]中的一个,我想要像这样的链接
  1. <a href="/auth/signup?cell=1654654654"></a>

我不知道如何在模板中传递细胞…

解决方法

如果你打算使用angula2 beta,那么你必须在做路由时发送这样的参数.
  1. <a [routerLink]="['signup',{cell:cellValue}]">Routing with parameter</a>
  2. <input type='text' [(ngModel)]='cellValue'>

而且在接收方面比你必须通过使用RouteParams得到参数.

如果您要使用angular2 RC而不是必须使用RouteSegment而不是在angular2 RC中使用RouteParams.像这样 :-

  1. import { Component } from '@angular/core';
  2.  
  3. import { Routes,RouteSegment,ROUTER_DIRECTIVES } from '@angular/router';
  4.  
  5. @Component({
  6. selector: 'about-item',template: `<h3>About Item Id: {{id}}</h3>`,Directives: [ROUTER_DIRECTIVES]
  7. })
  8.  
  9. class AboutItemComponent {
  10.  
  11. id: any;
  12.  
  13. constructor(routeSegment: RouteSegment) {
  14. this.id = routeSegment.getParam('id');
  15. }
  16. }
  17.  
  18. @Component({
  19.  
  20. selector: 'app-about',template: `
  21.  
  22. <h2>About</h2>
  23. <a [routerLink]="['/about/item',1]">Item 1</a>
  24. <a [routerLink]="['/about/item',2]">Item 2</a>
  25. <div class="inner-outlet">
  26. <router-outlet></router-outlet>
  27. </div>
  28. `,directives: [ROUTER_DIRECTIVES]
  29. })
  30.  
  31. @Routes([
  32.  
  33. { path: '/item/:id',component: AboutItemComponent }
  34.  
  35. ])
  36.  
  37. export class AboutComponent { }

猜你在找的JavaScript相关文章