When a user clicks "Get HTTP Data" button the getData()
method defined in the app.component
calls http
service's getData()
method that is supposed to return the json
data. But instead it returns the ZoneAwarePromise
object:
export class HttpService {
jsonFile = '../assets/products.json';
getData(): Promise<any> {
return this.http.get(this.jsonFile)
.toPromise()
.then(response => {
console.log(">>> .then got response:", response);
})
.catch(this.handleError);
}
Here is the link to the Stackblitz project:
https://stackblitz.com/edit/angular-ivy-ukgwct?file=src%2Fapp%2Fhttp.service.ts
How to fix this issue and make the http service to return the json data?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…