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
?