Angular: How to correctly implement APP_INITIALIZER
Do something like
export function StartupServiceFactory(startupService: StartupService) {
return () => startupService.load();
}
const APPINIT_PROVIDES = [
StartupService,
{
provide: APP_INITIALIZER,
useFactory: StartupServiceFactory,
deps: [StartupService],
multi: true,
},
];
Startup service
load():Promise{
return new Promise(resolve, reject){
//load all your configuration
resolve();
}
}
Due to how APP_INTIALIZER
works, it's expected that asynchronous initializers return promises, but your implementation of APP_INTIALIZER
multiprovider doesn't because loadConfigurationData
function doesn't return anything.
It should be something like:
loadConfigurationData(): Promise<Configuration> {
return this.http.get<Configuration>(`${this.originUrl}${this.configUrlPath}`)
.do(result => {
this.configData = result;
})
.toPromise();
}