我有一个ionic 2小应用程序,它使用存储在assets文件夹中的本地json文件。此文件提供了有关历史人物的简介数据,因此不需要在中进行编辑。我可以像这样访问home.ts中的文件
constructor(public navCtrl: NavController, public http: Http) {
this.http.get('../assets/people.json').subscribe(data => {
this.people = data.json();
});注意,这是可行的,我可以通过*ngFor循环遍历数据。
但我认为这应该成为一项服务。但我不知道如何将其放入服务中,然后访问我主页中的数据。我到处找,但找不到任何解决办法。
发布于 2018-06-02 22:11:41
多亏了this tutorial from TutorialsPoint,我才能做到这一点。本教程演示了如何创建服务,我将这些说明与ionic Storage结合起来,从本地存储的文件中设置和获取数据。
export class SomeService {
...
getSomething(): Observable<Something[]> {
return this._http.get(this._someUrl)
.map((response: Response) => <Something[]> response.json())
.do(data => this.storage.set("some_key", data));
}
...
}发布于 2017-03-01 15:23:17
Service.js
return this.http
.request('assets/data/radio-stations.json')
.map(response => response.json());contoller.js
this.service.getdata()
.subscribe(response => {
this.data = response.Radios;
}, err => {
console.log(err);
}); https://stackoverflow.com/questions/41786571
复制相似问题