函数在Angular的Firebase Auth中不起作用

时间:2019-02-21 12:11:17

标签: javascript typescript firebase firebase-authentication angular7

我正尝试在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 个答案:

答案 0 :(得分:0)

使用箭头函数(继承封闭的上下文),在您的代码中,回调绑定到其他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
  })