I have a component that relies on an API response for its content. I have set up the resolver but it still returns before my data is ready.
How can I make my pull_categories()
function wait until the response body is received and then return? Rather than returning an empty object because it won't wait or even call it in my case below.
service.ts
private _categories = [];
constructor(private http: HttpClient) { }
pull_categories() {
this.http.post('https://myapi.com/something', null)
.subscribe(
data => {
this._categories = data['response'];
return this._categories;
}
);
}
component.ts
categories = [];
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.data.subscribe(
(data: Data) => {
this.categories = data.categories;
}
);
}
resolver.ts
constructor(private categoriesService: CategoriesService) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {
return this.categoriesService.pull_categories();
}
app-routing.module.ts
{
path: '',
component: HomeComponent,
resolve: {categories: CategoriesResolverService}
}