2016-11-10 104 views
0

这是正常的这样或什么是最好的使用情况,以通话,因为没有别的工作如何使用离子储存得到?

this.storage.get('email').then((email) => { 
this.storage.get('token').then((token) => { 

    this.http.get('https://app.dev/clients.json?login='+email+'&client_token='+token) 
     .map(res => res.json()) 
     .subscribe(data => { 
     // we've got back the raw data, now generate the core schedule data 
     // and save the data for later reference 
     this.data = data; 
     resolve(this.data); 
     }); 
    }); 
}); 
}); 

我试图

data: any; 
token: string; 
phone:string; 
constructor(private http: Http, public storage: Storage) {} 


load() { 

this.storage.get('token').then((token) => { 
    this.token = token; 

}); 
this.storage.get('phone').then((phone) => { 
    this.phone = phone; 
}); 

    this.http.get('https://app.dev/clients.json?login='+this.phone+'&client_token='+this.token) 
      .map(res => res.json()) 
      .subscribe(data => { 
      // we've got back the raw data, now generate the core schedule data 
      // and save the data for later reference 
      this.data = data; 
      resolve(this.data); 
      }); 
     }); 

但this.phone =电话;未分配

我希望我可以叫this.storage.get(“手机”),并没有承诺。

+0

只能做'this.phone = this.storage.get('phone');'? – Huiting

+0

没有它的承诺。 – flakerimi

+0

你正在使用什么类型的存储? – Huiting

回答

1

由于它正在返回一个承诺,那么您的第一个代码片段将是最常见的方式。

我使用的离子2(2.0.0-beta.4)的旧版本仍使用的.js所以我的建议可能会或可能不会进行工作的版本。

import {LocalStorage} from 'ionic-angular'; 

constructor(...){ 
    this.local = new Storage(LocalStorage); 
    this.local.set("Key", "Key_Value"); 
} 

buttonClick(){ 
    this.key = localStorage.getItem('Key'); 
} 

这是目前我如何使用本地存储。只是一个建议,如果你不想处理承诺。

+0

是啊,我有本地存储,但离子存储只是支持更多的存储引擎。我想我会再多等一下如何从存储中获得最佳效果。 – flakerimi

+0

我不得不用纯javascript本地存储去。 window.localstorage.getItem() – flakerimi