javascript - Angular 4 : how do access local json?

标签 javascript json angular

在 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/

相关文章:

javascript - 为什么在有 componentDidUpdate 时使用 getDerivedStateFromProps?

javascript - 帧中的动画持续时间是什么意思?

javascript - 您可以使用 javascript (ECMAScript5) 中的对象文字表示法指定其他描述符属性吗?

javascript - 如何在 Angular 中根据变量名创建组件

javascript - Ionic Storage get 在重新加载页面之前不起作用

javascript - 我对 angularjs 嵌套 ajax 调用 [循环内循环] 的方法应该是什么?

javascript - ng-file-upload + 将多个文件数组空发送到服务器 (sails)

json - angularjs - ng-重复 : access key and value from JSON array object

json - 多个json字符串的分隔符

java - 如何使用 java Rest 服务捕获 Angular 2 项目中的 javascript 事件