繁体   English   中英

在路线上有角度的2传递参数

[英]angular 2 passing params in route

我有一个组件,我需要在点击按钮时将数据传递给另一个组件,所以我在锚点中使用了[routerLink]属性

<a [routerLink]="['/employeedetail' , name, address, 
detail3 , detail4, detail5, detail6 , detail7, detail8 , 
detail9, detail10></a>

在app.route.ts中定义了相应的路由

    {
    path: 'employeedetail/:name/:address/:detail3 /:detail4
    /:detail5 /:detail6/:detail7 /:detail8 /:detail9 /:detail10 ',
    component : employeedetailComponent
    }

在完美的世界中,它应该有效,但它没有给出错误的说法

zone.js:355Unhandled Promise rejection:纯函数的参数个数不受支持:11; 区域:; 任务:Promise.then; 值:错误:纯函数的参数个数不受支持:11(...)错误:纯函数的参数个数不受支持:11

我研究了这一点,发现当内联模板中有10个元素时,angular2路由器失败,我通过删除URL中的最后一个参数(detail10)尝试并且没有错误。

问题是如何使用[routerlink]在url中传递这些大量参数,或者我应该使用不同的方法将数据从一个组件传递到另一个组件?

您可以使用queryParams在URL中传递params。

<a [routerLink]="['/employeedetail' , name, address]" [queryParams]="{detail3: detail3, detail4: detail4}">Somewhere</a>

对于可选参数,使用查询参数更好,它使您的网址更具可读性。

向准备路径的组件添加方法并传递结果,而不是在模板中构建它

  class MyComponent {
    get employeeDetailLink() {
      return ['/employeedetail' , name, address, 
detail3 , detail4, detail5, detail6 , detail7, detail8 , 
detail9, detail10];
    }
  }

并使用它

<a [routerLink]="employeeDetailLink"

最近删除了这个限制(不知道它是否已经发布) https://github.com/angular/angular/pull/12710

另见https://github.com/angular/angular/issues/12233

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM