更新时间:2023-09-30 10:22:58
最终对我有效的方法是:
The approach that ended up working well for me is this:
let route: ActivatedRoute;
const newUrl = router.createUrlTree([
merge({'a': 123}, route.snapshot.params)
], {relativeTo: route});
通过使用合并,您可以添加,更新和减去url参数,然后使用router.navigateByUrl(newUrl)
来执行.
By using merge, you can add, update and subtract url parameters and then use router.navigateByUrl(newUrl)
in order to execute.
add: merge({newParam: 111}, route.snapshot.params)
update: merge({param: 111}, route.snapshot.params)
subtract: merge({param: null}, route.snapshot.params)
希望其他人觉得这和我一样有用.
Hope others find this as useful as I have.
另一个使用Object.assign而不是merge的示例:
Another example using Object.assign instead of merge:
let route = this.route; // The imported ActivatedRoute
let currentParamsObj: Params = Object.assign({}, route.params);
let newParam = {};
newParam[key] = value;
let newUrl = this._router.createUrlTree([
Object.assign(currentParamsObj, newParam)
], {relativeTo: this.route });
this._router.navigateByUrl(newUrl);