【发布时间】:2017-07-14 12:35:37
【问题描述】:
我使用 IONIC 2 创建了一个应用程序。我的所有页面都需要通过 REST API 加载,有时在没有更新的情况下在每个选项卡中重新加载是很烦人的。
现在我想通过在我的应用中实现缓存来改进这一点。我希望每个 http 请求都将在第一次后以当前时间戳保存,并在 2 小时后通过 REST Api 加载内容。
任何例子都会很棒。我尝试使用此插件https://github.com/Nodonisko/ionic-cache,但安装后出现一些问题,它显示错误。
我知道使用Sqlite会更好,但我不太确定并寻求专家的建议。
这是我的主页代码:-
import { WebService } from '../shared/services/web.service';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [ WebService ]
})
constructor(
public navController: NavController,
private webService: WebService ) {}
loadPosts() {
this.webService.getPosts(query)
.subscribe(data => {
key.posts = data;
loader.dismiss();
}, (err) => {
//Fail and log the err in console
loader.dismiss();
console.log("Some Issue");
let toast = this.toastController.create({
message: 'There is some issue with network',
duration: 10000
});
toast.present();
});
}
这是我的服务提供商页面:-
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Config } from '../../../../app/app.config';
import 'rxjs/add/operator/map';
@Injectable()
export class WordpressService {
constructor(private storage: Storage, private http: Http, private config: Config ) {}
public getPosts(query) {
query = this.transformRequest(query);
let url = this.config.webApiUrl + `/allposts?${query}&_embed`;
return this.http.get(url)
.map(result => {
return result.json();
});
}
}
谢谢桑尼
【问题讨论】:
标签: angular typescript caching ionic2