【问题标题】:Can't access my store from within Firebase promise [duplicate]无法从 Firebase 承诺中访问我的商店 [重复]
【发布时间】:2019-03-11 07:06:52
【问题描述】:

在附加到FirebasecreateUserWithEmailAndPassword() 函数的承诺中,我试图从我的商店访问突变函数,但出现以下错误:

"TypeError: 无法读取未定义的属性 '$store'"

为什么以及如何解决这个问题?

<script>
import firebase from 'firebase'

export default {
  name: 'Signup',
  data: function() {
    return {
      email: '',
      password: ''
    }
  },
  methods: {
    signUp: function() {
      firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(
        function(user) {
          alert('Your account has been created!');
          this.$store.userConnectedUpdate('true');
        },
        function(error) {
          alert('Oops. ' + error.message)
        }
      );
    }
  }
}
</script>

【问题讨论】:

    标签: firebase vue.js scope firebase-authentication


    【解决方案1】:

    回调函数this内部有不同的含义。对此有一些广泛的解决方案,但最简单的一种是使用=&gt; 表示法:

    signUp: function() {
      firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(() => {
          alert('Your account has been created!');
          this.$store.userConnectedUpdate('true');
        },
        function(error) {
          alert('Oops. ' + error.message)
        }
      );
    }
    

    在此处查看我的答案以了解其他选项:firebase :: Cannot read property 'props' of null

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 1970-01-01
      • 2020-02-05
      • 2015-09-11
      • 2021-06-05
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多