'Angular2: return type of function when implementing APP_INITIALIZER
I have created an Angular 12 Application to learn how APP_INITIALIZER works. Here is an extract of its app.module.ts
function initializeApp(appConfigService: AppConfigService) {
return () => appConfigService.load();
}
@NgModule({
declarations: [AppComponent, MyComponentComponent],
imports: [BrowserModule, HttpClientModule, FormsModule],
providers: [
AppInitService,
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [AppConfigService],
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: initializeApp1,
deps: [AppInitService],
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: initializeApp2,
multi: true,
},
],
bootstrap: [AppComponent],
})
export class AppModule {}
The app is up and running with appConfigService being called before the app is ,,running".
However, I am not very sure why in the initializeApp() function I have to return a function (otherwise the app would throw errors like below):
core.mjs:6484 ERROR TypeError: this.appInits[i] is not a function
at ApplicationInitStatus.runInitializers (core.mjs:24748:36)
at core.mjs:26040:28
at _callAndReportToErrorHandler (core.mjs:26143:24)
at core.mjs:26038:20
at _ZoneDelegate.invoke (zone.js:372:1)
at Object.onInvoke (core.mjs:25476:33)
at _ZoneDelegate.invoke (zone.js:371:1)
at Zone.run (zone.js:134:1)
at NgZone.run (core.mjs:25330:28)
at PlatformRef.bootstrapModuleFactory (core.mjs:26017:23)
My question is, that why I need a callback function in my initialzieApp() and cannot instead return the result of appConfigService.load() directly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
