Angular - append params to URL

亡梦爱人 提交于 2021-02-10 15:15:59

问题


I would like to append parameters to url:

 let params = [];
 params["p1"] = "value1";
 params["p2"] = "value2";

this.location.go(this.router.createUrlTree([this.data]).toString());

When I have this url localhost/project/page its work correct. But when I have url localhost/project/page/id after append url is localhost/project/page/p1=..

I need to keep the parameter in url and add the new one. thanks for advices


回答1:


When you click the button, call a method like the one below? Here this.router is Router instance, inject it in your component.

onClick(params) {
    this.router.navigate(['project', 'page', {id : id}], {queryParams: {p1: params['p1'], p2: params['p2']}});
}



回答2:


You can send params in the routing like below -

[routerLink]="['project/page','params']"

If you wish to send it as query params send it like this -

<a [routerLink]="['project/page']" [queryParams]="{ page: 99 }">Your caption</a>


来源:https://stackoverflow.com/questions/50717113/angular-append-params-to-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!