Check out this blog: https://www.cidean.com/blog/2019/initialize-data-before-angular-app-starts/
It uses APP_INITIALIZER
to hook AppConfigService
before app startup and fetch data. See the blog also for implementation of AppConfigService
.
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { AppConfigService } from './services/app-config.service';
export function appInit(appConfigService: AppConfigService) {
return () => appConfigService.load();
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
],
providers: [AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: appInit,
multi: true,
deps: [AppConfigService]
}],
bootstrap: [AppComponent]
})
export class AppModule { }