I use lazy-loading modules, when I arrive on a page of my module, I use functions of a service which performs actions on a global variable of this service.
I have a function that load this global variable from my service. The problem is that no matter where I call this function in my module, the variable is not filled fast enough on the first display, and so I get errors because my variable is empty on the first display.I'm
I tried loading my variable in my module's constructor, but it still doesn't fill the variable fast enough for the first display.
In my service :
export class ReferenceService {
parts: any;
constructor(
private http: HttpClient
) { }
loadParts() {
this.http.get(apiUrl.partsGet).subscribe(data => this.parts = data);
}
getParts() {
return this.parts;
}
}
In my module :
export class LaserfibreModule {
constructor(private apiReference: ReferenceService) {
this.apiReference.loadParts();
}
}
In my component :
console.log(this.apiReference.getParts()) // return undefined
I would like to find a way so that my component does not display until this variable "parts" is not filled
I found a temporary solution which is to call my "loadParts()" function in my app.component.ts, which works but if I do that, it's not really lazy loading anymore
I tried to use an APP_INITIALIZER in the provider of my module but it does not work