【发布时间】:2018-03-02 18:17:06
【问题描述】:
我在 Vue 组件中有一个登录方法,它使用 firebase 来登录用户。我正在使用计算属性 user、message 和 hasErrors。当这个方法运行时,它进入catch函数,但是出现了这个错误:
Uncaught TypeError: Cannot set property 'message' of undefined。我尝试直接更改 vuex 状态(因为这是计算道具所做的),但这会产生相同的错误。这是我正在使用的方法:
login: function (event) {
// ... more stuff
// Sign-in the user with the email and password
firebase.auth().signInWithEmailAndPassword(this.email, this.password)
.then(function (data) {
this.user = firebase.auth().currentUser
}).catch(function (error) {
this.message = error.message
this.hasErrors = true
})
// ...
}
这是计算出来的 prop 的样子:
message: {
get () {
return this.auth.message // mapState(['auth'])
},
set (value) {
this.$store.commit('authMessage', value)
}
}
我很确定问题与它位于Promise 中有关。
那么如何访问 firebase Promise 中的计算属性呢?
【问题讨论】:
-
this不是您认为的回调内部的内容 - 可能想了解this的工作原理以及如何解决它。
标签: javascript vue.js