繁体   English   中英

角2。 我该如何使用存在的参数添加附加的queryParam路由?

[英]angular2. how can I make a route append queryParam with exists params?

当我有URL时: http : //stack.com/abc?code=1而我想获得如下URL: http : //stack.com/abc?code=1&page=1

我想使用这种代码

let navigationExtras;
navigationExtras = {
  {preserveQueryParams: true},
  {queryParams: {page: 1}}
};

this.router.navigate(['/abc'], navigationExtras);

reserveQueryParams表示使用存在的参数。 和queryParams意味着附加新的参数。 但是,现在不能同时使用两者。

有什么好主意吗? 而不使用“ this.route.subscribe(...)”

如果您从ActivatedRoute复制它们(可以注入到构造函数中),则可以更改现有参数或附加新参数,而无需使用preserveQueryParams

const currentParams = this.activatedRoute.snapshot.queryParams;
this.router.navigate(["/abc"], {
   queryParams: {...currentParams, "page": "1"},
});

您应该启用NavigationExtras preserveQueryParams选项,并更正如下所示的对象。

let navigationExtras;
navigationExtras = {
  preserveFragment: true,
  preserveQueryParams: true, //added parameter
  queryParams: {page: 1}
};

this.router.navigate([this.route.url], navigationExtras);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM