本文介绍了在角度2中如何保留查询参数并添加其他查询参数以进行路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我在路线 / cars?type = coupe ,我想用其他查询参数导航到同一个端点(但保留现有的查询参数)。我正在尝试这样的事情

For example I am on route /cars?type=coupe and I want to navigate to the same endpoint with additional query params (but keeping existing one). I am trying something like this

<a [routerLink]="['/cars']" [queryParams]="{model: 'renault'}" preserveQueryParams>Click</a>

保留初始查询参数(type = cars)但忽略添加的(model = renault) 。这是预期/正确的行为还是某种错误?看起来preserveQueryParams优先于queryParams?还有其他顺利解决方案吗?

The initial query params are preserved (type=cars) but added ones (model=renault) are ignored. Is this expected/correct behavior or is some kind of bug? Looks like preserveQueryParams has priority over queryParams? Is there any other smooth solution?

推荐答案

在Angular 4+中, preserveQueryParams 已被弃用,转而支持 queryParamsHandling 。选项是'merge''保留'

In Angular 4+, preserveQueryParams have been deprecated in favor of queryParamsHandling. The options are either 'merge' or 'preserve'.

代码内示例(用于 NavigationExtras ):

this.router.navigate(['somewhere'], { queryParamsHandling: "preserve" });

模板内例子:

<a [routerLink]="['somewhere']" queryParamsHandling="merge">

这篇关于在角度2中如何保留查询参数并添加其他查询参数以进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:13