【发布时间】:2018-10-10 02:33:30
【问题描述】:
如何从 json 文件中获取 json 数据以及如何仅使用 angular 2 进行显示。我做了但没有工作。你能找到我做错的地方吗
https://stackblitz.com/edit/angular2-notifications-example-fkngjs?file=app/app.component.html
this.http.request('http://localhost:4200/app/jsondata/details.json?home=place&owner=person&minval=min&maxval=max').
subscribe((res) => {
let alldetails = JSON.stringify(res);
});
【问题讨论】:
-
在 stackblitz 上使用 localhost:4200 不起作用
-
该文件在本地可用吗?查询字符串是做什么用的?有没有可以在线获取这些数据的地方?
-
@gayathrin 检查我的答案,你错过了导入
-
@gayathri,你的 .json 文件必须在 assests 文件夹中,并提出关于 this.http.get('assets/details.json').subscribe(res=>this.alldetails=res 的请求)。看到使用 httpClient 你不需要使用 JSON.stringfy。 .json 必须在文件夹 assest 中(或在服务器中),因为当 angular-cli 构建项目时,您只会得到 index.html、一些“bundels 文件”(.js 和 css)和文件夹 assest。没有能力读取“localhost:4200/app/jsondata”