I am trying to figure out how to load my config before the component gets created and APP_INITIALIZE is not working. The structure is AppModule------> HeroesModule(HeroesComponent)
code:
app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HeroesModule,
HttpClientModule
],
entryComponents: [
HeroesComponent
],
providers: [],
bootstrap: []
})
export class AppModule implements DoBootstrap {
constructor(
private injector: Injector
) {
const customElement = createCustomElement(HeroesComponent, {injector: this.injector});
customElements.define('heroes-component', customElement);
}
ngDoBootstrap() { /* Keep empty */
}
}
heroes.module.ts
export function init_app(heroService: HeroService) {
return () => heroService.loadConfig();
}
@NgModule({
declarations: [HeroesComponent],
imports: [
CommonModule,
HttpClientModule,
],
providers: [
{ provide: APP_INITIALIZER, useFactory: init_app, deps: [HeroService], multi: true }
],
exports: [
HeroesComponent
]
})
export class HeroesModule { }
hero.service.ts
@Injectable({
providedIn: 'root'
})
export class HeroService {
private conf: any;
constructor(private http: HttpClient) {
}
loadConfig() {
const promise = this.http.get('/assets/some.json').toPromise();
promise.then(data => {
this.conf = data;
});
return promise;
}
get config(): string {
if (!this.conf) {
throw Error('error');
}
return this.conf.hero;
}
}
When I try to use the HeroService in my HeroesComponent the data is not loaded. How should I use the APP_INITIALIZER in this context ?