I am encountering an issue while using Angular resolver to fetch data before loading the template. The specific error I am facing is as follows: Here is the code snippet for my resolver:
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import { Router, Resolve, RouterStateSnapshot,ActivatedRouteSnapshot } from '@angular/router';
import { ApiService } from './api.service';
@Injectable()
export class ServiceResolve implements Resolve<any> {
constructor(private apiservices: ApiService, private router: Router) {}
resolve(route: ActivatedRouteSnapshot): Observable<any> {
let JsonVal = JSON.parse(localStorage.getItem('RegistrationSteps'));
let postData = {
'token': JsonVal.auth_token
}
let url = 'getUserDetails';
return Observable.fromPromise(this.apiservices.getUserDetailsByToken(postData,url));
}
}
And here is a snippet of my service code:
getUserDetailsByToken(data,url){
let headers = new Headers();
return this.http.post(this.base_url+url,data).map(ret=>ret.json());
}
When trying to retrieve the data in my component, I utilize the following code:
let userData = this.route.snapshot.data['userdata'];
console.log(userData,'sssss');
Below is how my route configuration looks like:
{path:'signup',component:VendorRegistrationComponent,resolve:{userdata:ServiceResolve}},
The error message I am receiving reads as follows:
error TS2345: Argument of type 'Observable' is not assignable to parameter of type 'PromiseLike'. Property 'then' is missing in type 'Observable'.