我试图从我的Angular2 src / app文件夹中读取json文件
目录结构如下: -
我尝试使用以下代码在app.config.js中读取该文件: -
public load() {
return new Promise((resolve, reject) => {
this._http.get("config.json")
.map(res => res.json())
.subscribe((data) => {
this.cache['config.json'] = new ConfigData(data.json());
resolve(true);
});
});
}
但我尝试的所有内容都会导致以下错误: -
zone.js:2224获取http://localhost:4200/config.json 404(未找到)
我需要将文件放在哪里?
我试过把它移到src目录无济于事。我也尝试过: -
应用程序/ config.json ./app/config.json
我还在应用程序下添加了一个资源文件夹,并将其添加到那里,但是它没有工作
我需要做什么?
由于
答案 0 :(得分:5)
将config.json文件放在assets文件夹中,并将请求更改为以下内容,
public load() {
return new Promise((resolve, reject) => {
this._http.get("assets/config.json")
.map(res => res.json())
.subscribe((data) => {
this.cache['config.json'] = new ConfigData(data.json());
resolve(true);
});
});
}
它应该工作。确保重新使用以使更改反映在www / assets文件夹中。