1

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.

Tushar
  • 1,948
  • 1
  • 13
  • 32

1 Answers1

3

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.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Can we set HashLocationStrategy on component's provider instead of modules provider? like this: `@Component({ providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }] })` – Tushar Nov 25 '19 at 14:25
  • 1
    This is only if you want to use the LocationService inside the component to normalize URLs. It will not work for your modules routing :( – Athanasios Kataras Nov 25 '19 at 14:36