Follow up question to Share data between components using a service in Angular2 and a response I got.
I'm trying to share data that a service providers retrieves via a GET. The component structure is as follows with the main routes going to CustomerPortalDashboardComponent and DocumentsComponent
<CustomerPortalBaseComponent>
<router-outlet>
<CustomerPortalDashboardComponent/>
<DocumentsComponent/>
<router-outlet>
</CustomerPortalBaseComponent>
CustomerPortalBaseComponent makes a call to get user info:
ngOnInit() {
this.customerPortalUserService.getUserInfo()
.subscribe(
(event: HttpEvent<any>) => {
switch (event.type) {
case HttpEventType.Sent:
break;
case HttpEventType.Response:
this.handleUserData(event.body)
}
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log("Client-side error occured.");
} else {
console.log("Server-side error occured.");
}
}
)
}
handleUserData(data: any){
this.userData = data;
}
CustomerPortalUserService
getUserInfo(){
interface ItemsResponse {
results: string[];
}
return this.http.get<ItemsResponse>(this.userUrl, {
observe: 'response',
headers: new HttpHeaders().set(),
})
}
What is the best way to get the userData in CustomerPortalDashboardComponent and DocumentsComponent and ensure that the data has been retrieved before the other components try and get it?