admin管理员组

文章数量:1289506

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

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
Share Improve this question asked Feb 20 at 12:32 Mad EddieMad Eddie 1,2273 gold badges15 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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]
};

本文标签: electronAngular conditional provideRouter featuresStack Overflow