I have a service AuthenticationService that loads AngularFirestore. The service is loaded in RootComponent. All app modules are lazy-loaded in RootComponent (it has the main router-outlet). Now there are many sub-modules that also load AngularFirestore.
I have to ensure that AuthenticationService is initialized (async stuff) before the components and modules are loaded up, so I have put it in APP_INITIALIZER provider. This causes cyclic-dependency Cannot instantiate cyclic dependency! AngularFirestore.
If I don't put it in APP_INITIALIZER it works but the app runs without AuthenticationService being initialized.
Is there a way around this? (besides adding auth.initialize() in all components)
App tree:
AppComponent -> RootComponent(AuthenticationService) -> All Submodules(AuthenticationService, AngularFirestore)
// constructor of AuthenticationService
constructor(
private auth : AngularFireAuth,
private remoteconfig : AngularFireRemoteConfig,
private keepalive: Keepalive,
private idle: Idle,
private toast : ToastService,
private router : Router,
private fs : AngularFirestore,
private http : HttpClient,
)
// providers in app.module.ts
{
provide: APP_INITIALIZER,
multi: true,
useFactory: authFactory,
deps: [
AuthenticationService
]
}
// Factory for APP_INITIALIZER
export function authFactory(
auth : AuthenticationService
) {
return () : Promise<any> => auth.initialize();
}
Regards!