当前位置: 代码迷 >> JavaScript >> 函数在Angular的Firebase Auth中不起作用
  详细解决方案

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

热度:52   发布时间:2023-06-05 09:35:21.0

我正尝试在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

使用箭头函数(继承封闭的上下文),在您的代码中,回调函数绑定到一个不同的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
  })
  相关解决方案