2016-12-09 116 views
2

我正在开发一个Ionic2应用程序,我使用的是承诺。在一个打字稿I类已经创建了这个方法承诺的空对象Angular2

tapOnRegistrati() { 
this.authService.userRegistration(this.email,this.password).then(function(user){ 

    this.user = this.af.database.object('/users/' + user.uid); 
    this.user.set({ name: this.name, lastname: this.lastname}); 


}).catch(function(error: any) { 

}); 

} 

但是当执行thenthis的参考是null,所以不能使用我打字稿类的任何对象/属性。为什么?

回答

1

因为如果你使用像

.then(function(user){ 
    this.user = this.af.database.object('/users/' + user.uid); 
    this.user.set({ name: this.name, lastname: this.lastname});  
}) 

this回调函数将把回调内部函数对象。如果您想参考该页面,请使用:

then((user)=>{ 
    this.user = this.af.database.object('/users/' + user.uid); 
    this.user.set({ name: this.name, lastname: this.lastname}); 

})