4

is it possible to have some of the pages using the PathLocationStrategy and some the HashLocationStrategy?

For example the login will not have any hash and the rest of the pages will have a hash in the URL.

Currently I do not see an easy way to do this. It is either all of them with or without a hash like the example below:

@NgModule({
    imports: [
        RouterModule.forRoot(LAYOUT_ROUTES, { useHash: false , enableTracing: DEBUG_INFO_ENABLED })
    ],
    exports: [
        RouterModule
    ]
})

Thank you

Investigator
  • 1,431
  • 2
  • 17
  • 24

1 Answers1

1

I found exactly what I was looking for here: Angular2 - APP_BASE_HREF with HashLocationStrategy

I used a CustomLocationStrategy and changed the URL based on my needs

import { Injectable } from '@angular/core';
import { HashLocationStrategy } from '@angular/common';

@Injectable()
export class CustomLocationStrategy extends HashLocationStrategy {
    prepareExternalUrl(internal: string): string {
        let url = this.getBaseHref() + '/#' + internal;
        if (internal === '/login') {
            url = this.getBaseHref() + internal;
        }
        return url;
    }
}

and the index.html

...
    <base href="./" />
...
Investigator
  • 1,431
  • 2
  • 17
  • 24
  • I don't understand how to get it to work. Basically, my requirement is easy: 1. I want to use `HashLocationStrategy`; 2. I want exactly one path w/o hash in URL (`http://.../sitemap.xml`); All my routes (inclusive sitemap) are specified w/o any hashes). When I invoke the sitemap route with the described solution, no route matches. Am I the only one with that requirement? I simply want to dynamically generate a sitemap.xml based on the specified routes. – PAX Jun 26 '23 at 09:13