Home > Software engineering >  How to replace or update query param in route URL?
How to replace or update query param in route URL?

Time:12-22

I want to replace or update the second param of a URL.

My URL is this way edit/id/test-page/id2. I want to replace/update the value of id2 when a button is clicked. I tried the following but few of them are adding new query params to the existing URL, and one example was appending the value to id param.

  1. this.activatedoute.navigate([ this.route.url ], { queryParams: {id: this.id1, id2: this.id2} })

  2. this.activatedoute.navigateByUrl(this.route.url);

Any help is appreciated

CodePudding user response:

You can specify relative path using relativeTo property

Try this

constructor(private router:ActivatedRoute,private router:Router){}   

this.router.navigate([],{relativeTo:this.activatedRoute, queryParams: {id: this.id1, id2: this.id2} })

CodePudding user response:

After many trials and errors, I have found the solution

constructor(private router: Router)

test(id2){
  this.router.navigate([`/edit/${this.id}/test-page`, id2]);
}
  • Related