Ben*_*don 6 typescript angular-routing angular
我在我的应用程序中使用查询参数,我想在导航和更新设置时保留/更新.通过执行以下操作,我可以毫无问题地添加参数.
onNavigate() {
this.router.navigate(['reports'], {queryParams: {'report': this.chart}, preserveQueryParams:true});
}
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够更改报告参数,并在我的应用程序中将新的参数添加到网址中.
使用preserveQueryParams: true使我的参数只读.我希望能够更新当前的params并添加新的params而不会丢失任何东西,除非我清除它们.
如何在不丢失当前设置的参数的情况下执行此操作?
我想这并不意味着代表应用程序preserveQueryParams追加/更新。queryParams
您必须编写逻辑来获取现有查询参数并根据您的要求更新它,然后在导航时通过。
this.route.queryParams.subscribe(qparams => {
this.currentQueryParams= qparams;
});
updatedqueryParams = // use this.currentQueryParams update it to append\update new stuff
let navigationExtras: NavigationExtras = {
queryParams: updatedqueryParams
};
// Navigate to the login page with extras
this.router.navigate(['<some path>'], navigationExtras);
Run Code Online (Sandbox Code Playgroud)
目的preserveQueryParams是传递您正在路由到的当前全局查询参数,
所以如果当前 URL 是
dashboard?debug=true&somevar=1
Run Code Online (Sandbox Code Playgroud)
然后你写下类似下面的内容,
this.router.navigate(['admin'], {
preserveQueryParams: true
}
);
Run Code Online (Sandbox Code Playgroud)
新路线将是,因此它保留查询参数。
admin?debug=true&somevar=1
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
5102 次 |
| 最近记录: |