I'm using APP_INITIALIZER hook to load some app configuration from a local json file. After the loaded configuration, I want to use it in another provider (setting BASE_PATH for my generated swagger-codegen services).
I get this error: Uncaught TypeError: Cannot read property 'api' of undefined
The value I try to access AppInitService.settings.api is a static property on AppInitService. I suspect this is the problem, but as I understand the APP_INITIALIZER hook should guarantee to wait before continuing...
How do I wait for the value to be loaded in APP_INITIALIZER and ready to be reused in another providers useValue?
app.module.ts
export function initializeApp(appConfig: AppInitService) { return (): Promise<any> => appConfig.init() } @NgModule({ declarations: [...], imports: [...], providers: [ AppInitService, { provide: APP_INITIALIZER, useFactory: initializeApp, deps: [AppInitService], multi: true, }, { provide: BASE_PATH, useValue: AppInitService.settings.api, }, ], app.config.service.ts
@Injectable() export class AppInitService { static settings: IAppConfig private conf: IAppConfig constructor(private http: HttpClient) {} public getConf() { return this.conf } init() { const jsonFile = `assets/config/config.json` return this.http .get(jsonFile) .pipe( tap((returnedConfig: IAppConfig) => { console.log('returnedConfig', returnedConfig) AppInitService.settings = returnedConfig this.conf = returnedConfig }) ) .toPromise() } }