Search code examples
angularangular-router

How to keep hashtag in Angular


I'm developing in Angular and every time I'm manually typing an url, the hashtag did get removed.

Example :

mywebsite.com/some/url#anchor-id
When I type enter, then it does change it to mywebsite.com/some/url

I looked and I don't know why Angular is removing this hashtag.
Is it something in the configuration of my project that I have to change ?

Already had a look at useHash: true but this isn't helping.


Solution

  • After a long search...

    Angular doesn't have any configuration for that.

    The problem

    On each route, I did add the language of the page to the location -> mywebsite.com/some/url will became mywebsite.com/en/some/url.

    This was the code

    this.location.replaceState(`/${this.selectedLang}${this.location.path()}`)
    

    The solution

    location.path() method does accept a variable -> includeHash?: boolean | undefined
    passing true -> location.path(true) will then return the expected url.

    So, this is the solution...

    this.location.replaceState(`/${this.selectedLang}${this.location.path(true)}`)