问题描述
我正尝试在Angular7应用中使用Firebase Auth。 我想使用一些功能来处理用户数据。 但是,在我使用google帐户登录后,直到myFunction可以工作之前都进行编码,但是在myFunction不起作用之后,我不确定根本原因,因为未显示任何消息。
你能给我建议吗?
constructor(private afAuth: AngularFireAuth) {}
public myFunction(obj: any){
console.log(obj)
}
public socialLogin() {
var provider = new firebase.auth.GoogleAuthProvider();
this.afAuth.auth.useDeviceLanguage();
this.afAuth.auth.signInWithPopup(provider).then(function(result) {
console.log('logged in'); <- work!
var user = result.user; <- work!
console.log(user) <- work!
this.myFunction(user); <- not work
console.log('test'); <- not work
})
打字稿版本为2.9.2
1楼
Karim
0
已采纳
2019-02-21 12:15:12
使用箭头函数(继承封闭的上下文),在您的代码中,回调函数绑定到一个不同的this
this.afAuth.auth.signInWithPopup(provider).then(result => {
console.log('logged in'); <- work!
var user = result.user; <- work!
console.log(user) <- work!
this.myFunction(user); <- not work
console.log('test'); <- not work
})