javascript - Angular 2在 route 传递参数

标签 javascript angular angular-routing routerlink

我有一个组件,我需要在点击按钮时将数据传递给另一个组件,所以我在这样的 anchor 中使用了 [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: Unsupported number of argument for pure functions: 11 ; Zone: ; Task: Promise.then ; Value: Error: Unsupported number of argument for pure functions: 11(…) Error: Unsupported number of argument for pure functions: 11

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

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

最佳答案

您可以使用 queryParams 在 url 中传递参数。

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

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

关于javascript - Angular 2在 route 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768108/

相关文章:

Angular2+ routeReuseStrategy 生命周期钩子(Hook)

php - 使用 html 和 JavaScript 的表单创建测验

javascript - 如何在不重新输入的情况下将所有 JavaScript 和 css 文件加载到 WordPress function.php

javascript - 将外部 HTML 添加到 React 组件不起作用

javascript - 使用 angular.js 路由手动刷新时仍然得到 'Not Found'

angular - angular2中的动态路由

javascript - 存在值时更新变量

Angular 如何测试@HostListener

angular - 在 dart 中与 typescript 互操作

angular - 使用 XLSX(表格到工作表)导出 html 表格时隐藏/删除列