缓存离子2首先加载不解析数据

时间:2017-05-18 19:28:53

标签: caching ionic-framework promise ionic2

我使用插件ionic-cache来缓存离子2应用中的所有请求。问题是第一次没有可用的缓存时数据无法解析。第二次加载数据可用,应用程序正常工作。

我的api服务:

getPosts(category, count) {

        return new Promise(resolve => {    
            let key = `${URL.getPosts}?post_type=post&count=${count}&cat=${category}`;
            this.cache.getItem(key)
            .catch(() => {
                // fallback if item is expired or doesn't exist
                return this.http.get(key)
                    .map(res => res.json())
                    .subscribe(data => {
                        // save response to cache
                        this.cache.saveItem(key, data);
                        this.data = data;
                        resolve(this.data);
                });
            }).then((data) => {
                 this.data = data;
                 resolve(this.data);
            });
        });
    }

我的pageComponent调用apiservice:

loadPosts(){

    let count = 15;
    let category = 24;

    this.GetApi.getPosts(category,count).then(
        data => {
            this.create( data );
        }
    );
}

create(data){
    this.items = data.posts;
}

0 个答案:

没有答案