我有一个名为data.json的json文件存储在assets文件夹中。
我正在尝试这样做,以在service.ts文件中获取数据。
{
"response": {
"sales_amount": 0,
"collection_amount": 0,
"carts_amount": 736.63,
}
}
我正在尝试这样做,以在service.ts文件中获取数据。
my_data: any;
public getResponseData() : Promise<any> {
if(typeof(this.my_data) === "undefined") {
return this.http.get('assets/data.json')
.toPromise().then(res => {
this.my_data = res.json().response;
return this.my_data;
}).catch(this.handleError);
} else {
return Promise.resolve(this.my_data);
}
}
我在这里获得了响应,保存在this.my_data中,但在component.ts文件中,我是这样做的:
ngOnInit(){
this.myService.getResponseData().then(response => this.detailsdata = response);
}
但是在 this.detailsdata 中我没有得到任何东西。
之后,我想在 HTML 文件中显示 sales_amount 和 collection_amount。
<div>
<span>sales amount</span>
</div>
我想要显示数值,而不是销售金额。如何做到这一点?我对TypeScript非常陌生,有谁可以帮助我吗?