我很努力應付處理我的應用程序,這是寫在Angular2/ionic2本地存儲數據的異步檢索。鏈接承諾,以應付來自this.storage.get()調用異步承諾
我的代碼如下:
request(args) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
// Retrieve the token from the local storage, if available
this.storage.get('token').then((value) => {
headers.append('Authorization', 'Token ' + value);
});
args = args || {};
var url = this.API_URL + args.url;
var method = args.method;
var data = args.data || {};
var options = new RequestOptions({headers: headers, withCredentials: this.use_session});
return new Promise((resolve,reject) => {
if (method == 'GET') {
this.http.get(url, options)
.map(res => res.text())
.subscribe(
(data:any) => {
resolve(data);
},
(err:any) => {
data = {};
data['status'] = 0;
data['non_field_errors'] = ["Could not connect to server. Please try again."];
data['error'] = err;
reject();
},
() => {
}
);
}
});
}
調用請求函數的代碼如下所示:
authenticationStatus(){
return this.request({
'method': "GET",
'url': "/user/"
})
.then((data:any) => {
console.log('****** authenticationStatus: GET /user/ SUCCESS');
});
}
,這是又被稱爲是這樣的:
this.djangoAuth.authenticationStatus()
.then((data:any) => {
this.loggedIn = true;
this.enableMenu(true);
this.root = PhotoPage;
},
(err:any)=>{
this.loggedIn = false;
this.enableMenu(false);
this.root = LoginPage;
});
我有困難的包裹我的頭周圍嵌套的承諾,不能工作了如何修改上面的代碼,以便this.storage.get("token")
調用的結果在下面的代碼中使用之前已將數據添加到標題中。我認爲我需要以某種方式將承諾鏈接起來,但不能確切地說明如何。
本地localStorage的訪問添加
else
部分是同步的......不知道爲什麼你的被包裹在承諾 – charlietfl這不是原生它是angularjs /離子 –
這兩個框架都使用本機javascript – charlietfl