angularhash-location-strategy

Angular 5 avoid HashLocationStrategy for specific route


In my angular 5 application I have multiple routes. I have added { provide: LocationStrategy, useClass: HashLocationStrategy } in providers of app.module.ts file. But I want to remove this HashLocationStrategy from specific route.

Thank you.


Solution

  • Long story short, you can't. The why is infered by the documentation and the way you declare the routing strategy:

    A LocationStrategy used to configure the Location service to represent its state in the hash fragment of the browser's URL.

    It's provided (like a service) and there is no way to hook into injection and provide multiple injection options in runtime (if path is like ... then inject this or the other). Dependency injection is setup during module initialization and there is no way around that, that I know.

    Then we can infer from the purpose of the Location service, which is only one and is to create the relative urls based on the strategy, that it takes the provided stategy and uses it application wide.