0
參考
我的服務具有以下代碼:角2`this`在承諾
....
isAuthenticated(){
var tt = this;
if(tt.current_user){
return Promise.resolve(tt.current_user,false);
}else{
return new Promise(resolve => {
this.http.get(this.api.urlBase+this.api.apiBase+'/Individuals/me',
{withCredentials: true})
.map(res=>res.json())
.subscribe((data) => {
tt.current_user = data;
resolve(data,false);
},(err)=>{
reject(err,true);
});
})
}
}
....
而在我的頁面類,我試圖訪問它:
constructor(){
...
let tt = this;
individual.isAuthenticated().then(this.handleLogin);
...
}
...
handleLogin(d,err){
console.log("This: ",this);
console.log("This: ",tt);
this.nav.present(this.loading);
}
但在handleLogin
, this.nav
拋出錯誤,this
未定義,並且控制檯日誌顯示this
爲空白,tt
爲未定義。如何從該函數中引用this
?