我正在尝试从我的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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
但我尝试的一切都会导致以下错误: -
zone.js:2224 GET http:// localhost:4200/config.json 404(未找到)
我需要在哪里放置文件?
我已经尝试将它移动到src目录无济于事.我也尝试过: -
app/config.json ./app/config.json
我还在app下添加了一个assets文件夹并将其添加到那里,但这也没有用
我需要做什么?
谢谢
将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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
它应该工作.确保重新使用以使更改反映在www/assets文件夹中.