I have an Angular (18.2) + Electron (26) application which requires the HashLocation whereas when running on the web does not need this.
Within the standard app.config.ts I simply use the below for Electron
provideRouter: (routes, withHashLocation())
How can I make this dynamic so that withHashLocation is not added when not Electron?
The only way to check if the Angular site is running under Electron is with the below statement which feels like it isn't accessible when the app.config.ts is loaded.
this.windowReference.api && this.windowReference.api.isElectron
You could try to export the route providers to a separate variable and conditionally assign it based on the environment like this (inside app.config.ts
):
const isElectron = this.windowReference?.api?.isElectron; // may have to use window?.api?.isElectron since app.config.ts is a module.
const routerProviders = isElectron ? provideRouter(routes, withHashLocation()) : provideRouter(routes);
export const appConfig: ApplicationConfig = {
providers: [routerProviders]
};