How to replace a history instead a pushing a new in angular 2's new router (rc.1)?
For example Im in a question list (/questions
) opening a new modal in a new route (/questions/add
), and after adding a new question I go to the question view (/questions/1
). If I press back I would like to go to the /questions
instead of /questions/add
You want to use replaceState from the Locations class
https://angular.io/docs/ts/latest/api/common/index/Location-class.html#!#replaceState-anchor
If you use
And then
With the exact same path, you can trigger changes that usually happen, as well as replacing the history.
For example, in your case:
If you need to add parameters to the route, you can create the url for location using
In your example:
Update
It seems the angular router now comes with a replace url option. In your example:
Update 2
There is a current issue where multiple navigations done in a short amount of time may not replace the URL correctly. As a temporary workaround, wrap the navigate function in a timeout to get each to fire in a separate cycle: