【发布时间】:2019-05-29 12:48:19
【问题描述】:
我正在尝试在 Ionic 3 中读取本地 JSON 文件。我已将资产文件夹中的 JSON 文件保存为 csvjson.json
我在其中一项服务中调用以下函数。
getProducts() { console.log('内部 getProducts')
return this.http.get(this.apiHost)
.map((response: Response) => {
console.log(response);
return response.json();
});
}
然后将结果存入
myArray = this.databaseprovider.getProducts();
console.log("Returned from getProducts:" + myArray.length);
但是我得到的输出为
从 getProducts 返回:未定义
你能建议我哪里出错了吗?
【问题讨论】:
-
从上面的代码中没有得到你到底在做什么解释更多
-
在 http.get 中不再需要调用 map 了,因为它已经返回了一个 json。因此我使用了下面的代码。
this.http.get<any[]>('assets/csvjson.json') .subscribe(data => { data.forEach((item) => { console.log(item); console.log(item.Category);
标签: json angular ionic-framework