首页 > 解决方案 > Angular 5 将附加值作为查询参数传递

问题描述

formNavigation.ts(共享模块组件)中,我有:

@Input nextPath?:string

next() {
    this.router.navigate([this.nextPath], {queryParamsHandling: "merge"})
}

comp1.component.html

<form-navigation [nextPath]="'enroll/plans'"></form-navigation>

上面的代码工作正常,我正在通过注册/计划页面中的激活路由获取查询参数。

但我想传递一个附加值(索引 = 0)作为查询参数

<form-navigation [nextPath]="'enroll/plans'" {index:0}></form-navigation>

我尝试将上面的代码发送 index = 0 来注册/计划,但它显示错误。

标签: angular

解决方案


首先,如果你想传递查询参数,你必须这样做

this.router.navigate([this.nextPath], { queryParams: { x : val} } );

然后定义Input参数,并将其绑定到您的html中。

所以你的最终代码看起来像这样

@Input() index: number;

this.router.navigate([this.nextPath], { queryParams: { index : this.index} } );

<form-navigation nextPath="enroll/plans" [index]="0"></form-navigation>

推荐阅读