在 Angular2 中,你可以有一个文件夹/data/和一个 json 文件,你可以在 localhost:4200/data/something.json 访问它。
这在 Angular4 中不再可能。
知道如何让它工作吗?
最佳答案
你可以使用这个代码
@Injectable()
export class AppServices{
constructor(private http: Http) {
var obj;
this.getJSON().subscribe(data => obj=data, error => console.log(error));
}
public getJSON(): Observable<any> {
return this.http.get("./file.json")
.map((res:any) => res.json())
.catch((error:any) => console.log(error));
}
}
这里的 file.json
是你本地的 json 文件。
另见此处
还可以查看 angular-cli 路径的变更日志
关于javascript - Angular 4 : how do access local json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275995/