如何将VueJs方法范围绑定到返回的Firebase Promise?为了调用VueJS Modal
login: function () {
fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password)
.then(function (user) {
if (!user.emailVerified) {
this.$modal.show('email_verification', {
text: 'Please verify your email'
})
}
}, function (error) {
console.log(error.message);
})
}
现在,这是错误:
TypeError:无法读取未定义
的属性'$ modal'
答案 0 :(得分:1)
在promise回调中使用箭头函数,以便this
以词法方式绑定
login: function () {
fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password)
.then( (user) => {
if (!user.emailVerified) {
this.$modal.show('email_verification', {
text: 'Please verify your email'
})
}
}, (error) => {
console.log(error.message);
})
}
或者声明var self
将vue实例指向方法内部并使用self
来使用vue实例,因为回调现在将对var self
变量有一个闭包
login: function () {
var self = this;
fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password)
.then(function (user) {
if (!user.emailVerified) {
self.$modal.show('email_verification', {
text: 'Please verify your email'
})
}
}, function (error) {
console.log(error.message);
})
}