1

I would like to import a module in my project with a certain configuration loaded from a file, that configuration file could be modified at runtime and the application should be loaded with the new changes when the browser is refreshed.

What I am doing now is to send the configuration file path to the module in the "forRoot" method and providing the configuration with the "useFactory" that points to a "load" method, this "load" method is just using an HTTP GET to retrieve my configuration file. Everything seems to be working fine with "ng build" until I execute "ng build --prod --build-optimizer", that is causing an "Error encountered resolving symbol values statically".

I suppose its cause is because I am using a provider with "useFactory" inside static "forRoot" method and that "useFactory" is pointing to a non-static method. How is it possible to deal with this? is this a good approach?

This is how my 'forRoot' method and 'initConfig' method looks like:

export class JwtSecurityModule {
   public static forRoot(configPath:string) {
      return {
         ngModule: JwtSecurityModule,
         providers: [
            AppConfig,
            {
               provide: APP_INITIALIZER,
               useFactory: initConfig,
               deps: [AppConfig, configPath],
               multi: true
            }
         ]
      };
   }
   ...
}
...
export function initConfig(config:AppConfig, configPath: string) {
   return () => config.load(configPath);
}   
Alejandro Morán
  • 669
  • 1
  • 13
  • 36

1 Answers1

1
export function initConfig(config:AppConfig, configPath: string) {
  return (): Promise<any> => {
    return new Promise((resolve, reject) => {
      config.load(configPath)
      resolve();
    });
  };
}

As far as I know, you need to return a Promise there.

Perhaps, Angular 5 is not running my APP_INITIALIZER at the moment :(

Mateo Tibaquira
  • 2,059
  • 22
  • 23