簡體   English   中英

將數據保留在ionic2中

[英]Keep data from services in ionic2

我想保留來自ionic2服務中的數據。 現在,每次從API加載數據時,我都不希望每次用戶訪問視圖時都調用它。

public getUpcomingShoots(){

        var json = JSON.stringify({ });
        var headers = new Headers();
        headers.append("Accept", 'application/json');
        headers.append('Content-Type', 'application/json' );
        let options = new RequestOptions({ headers: headers });

        return this.http.post(url, json, options)
        .map(data => data.json());

  }

我的功能是:

getUpcomingShoots (){
    this.showLoading();
    this.mainService.getUpcomingShoots().subscribe(success => {


      if(success.status == 1){
        this.loading.dismiss();
        console.log(success);

        if(success.data.length == 0 ){
          this.noUpcomingEvents = true;
        }else{
          this.noUpcomingEvents = false;
        }

        this.upcomingEvents = success.data;

      }else{
        this.loading.dismiss();
        console.log(success);

      }
    },
    error => {
      console.log( error);
    });
  }

這取決於您想要什么。 現在,每次用戶打開視圖時都將獲取數據。

解決該問題的最佳方法可能是將調用移至ionViewDidLoad生命周期事件中。 從文檔:

ionViewDidLoad: Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The ionViewDidLoad event is good place to put your setup code for the page.

class MyPage {
  ionViewDidLoad() {
    // Add your API call here
  }
}

您可以在這里了解更多信息: https : //ionicframework.com/docs/v2/api/navigation/NavController/

您可能仍想向服務中添加某種類型的緩存,這些緩存將在一段時間內返回緩存的值,除非您明確強制刷新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM