我有一個存儲設置爲ACCESS_TOKEN,當我得到它回到我第一次失敗離子存儲返回undefined第一次
提供商:
getLayMinisters(){
console.log(this.access_token);
return this.http.get(
this.api.url() + '/faith-leader/' + 'lay-ministers',
{
headers: new Headers({ "Authorization": "Bearer " + this.access_token })
}
)
.map(
response => response.json()
);
}
這是該文件
構造access_token: any;
layMinister_id: string;
constructor(
public http: Http,
public storage: Storage,
public api: ApiProvider
) {
this.storage.get('access_token').then(
(access_token) => {
this.access_token = access_token;
}
)
}
這是我第
getLayMinisters(){
this.layMinisterProvider.getLayMinisters().subscribe(
data => {
this.layMinisters = data;
}
);
}
這是它上面
layMinisters = [];
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public layMinisterProvider: LayMinisterProvider,
public alertCtrl: AlertController
) {}
ionViewDidLoad() {
this.getLayMinisters();
}
ionViewWillEnter() {
// this.getLayMinisters();
}
當頁面第一次運行時,它返回console.log(this.access_token)
收益爲undefined
,然後我得到的API的錯誤沒有響應。但是,當我再次單擊鏈接打開頁面時,它運行良好並打印access_token並運行api調用並打印所有數據
'this.storage.get'是一個異步操作。這就像'setTimeout(()=> {},2000)' – echonax