0

1.) let's suppose in our routing module we have a path configured like this:

{ path: 'version/:version/system/:system/id/:id' }

2.) in the browser we navigate to:

version/foo/system/bar/id/123

3.) in a component we want to change the URL to:

version/baz/system/bar/id/123

...the version changed...all other params are the same...

...we don't know how to achieve this (hopefully no string replacement :D ) with just changing the params of the router for example and appreciate any help or input...

some additional information:

this.route.params.subscribe(routeParams => {
  this.routeParams = routeParams;
})

this.routeParams would be something like that

{version: 'foo',system: 'bar',id: '123'}

in my component i want to give version a new value like 'baz'

{version: 'baz',system: 'bar',id: '123'}

i know this would throw an error without creating a new object because this.routeParams is readonly...so i tried different things like

this.router.navigate([this.router.url], {version: 'baz',system: 'bar',id: '123'})

i can easily create a new URL string with the information i have but maybe there is a simple angular way i cant see :D

AT82
  • 71,416
  • 24
  • 140
  • 167
dr7p
  • 1
  • 3

1 Answers1

0

Use your router and navigate to the current route but with the changed parameter like:

this.router.navigate([MyCurrentRoute, { version: 'baz' }]);
Tobias
  • 873
  • 9
  • 17
  • correct me if i'm wrong but wouldn't that cause the route to be something like: {MyCurrentRoute};version=baz instead of replacing the actual value? but this is exactly what i try to achieve...passing the actual route and change it with a new param config object... – dr7p Mar 17 '17 at 18:13
  • That might be true actually, I only tested with one route param. Take the previous params object and replace version (if available) and then navigate to the current route. We use it like that to change current language which is a route param. – Tobias Mar 17 '17 at 18:45