微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Angular4:如何访问本地json?

@H_404_0@在Angular2中,你可以在那里有一个文件夹/ data /和一个json文件,你可以在localhost:4200 / data / something.json访问它.

@H_404_0@Angular4中不再可能这样.

@H_404_0@任何想法如何让它工作?

解决方法:

@H_404_0@你可以使用这段代码

@H_404_0@

@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));

     }
}
@H_404_0@这里file.json是你的本地json文件.

@H_404_0@也见到这里

@H_404_0@> How to get a json file in angular2 using the Http class

@H_404_0@也看到了路径的角度cli的变化

@H_404_0@> https://github.com/angular/angular-cli/blob/master/CHANGELOG.md#100-rc4-2017-03-20

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐