I would like to change the url of my page, but without changing the angular history state nor reloading the page.
Is this somehow possible ?
Already tried
| Method | no history changes | not reloading | changing the url |
|---|---|---|---|
location.replaceState(url) |
not working | working | working |
router.navigateByUrl(url, {skipLocationChange: true, }) |
working | not working | working |
A bit of context
I'm adding the /de at the beginning of the url -> so from /my-url to /de/my-url.
But actually, the existing route is /my-url so for navigation through my app I've added a guard that.
- Control the url
- If it does have the
/deremove it androuter.navigate(urlWithoutDe) - Add it back again to visually see the
/de(or any other language like/en,/fr, ...) - But when the user navigate back with the back button, the
guardwont be triggered, so it will try to navigation to/de/my-urlwhich does not exists..
Did you try listening to the
beforeunloadevent in yourapp.component, something like this:https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent
As a general note, Angular Guards will not be triggered when the user clicks on the back/forward buttons, you need to listen for this event.