I have two component panelComponent(parent) and dashboardComponent(child). The panelComponent has a subscription to a service in its ngOninit and its return value is used inside childcomponent ngOninit.
The Problem is, on initial running my childs ngOninit executing before Parents service returns a value and I am getting a null result in child component. How to solve this issue?
panelComponent.ts:-
ngOnInit() {
this.panelService.getUserProfile().subscribe(
res => {
this.panelService.userDetails = res;
this.router.navigate(['panel/dashboard'])
},
err => {
console.log(err);
},
);
}
panelService.ts:-
export class PanelService {
userDetails;
constructor(private http: HttpClient) {
}
getUserProfile() {
const httpOptions = {
headers: new HttpHeaders({
'Authorization': `Bearer ${localStorage.getItem('token')}`
})
};
return this.http.get('/api/UserProfile/UserProfile', httpOptions);
}
dashboardComponent.ts:-
ngOnInit() {
console.log(this.panelService.userDetails)
}