如何解决 vue 3 中的错误 "this is undefine"
how to solve error "this is undefine" in vue 3
我已经创建了一个 vue 组件,如下所示:
<template>
<div class="login">
<h3>Sign in</h3>
<input type="text" placeholder="Email" v-model="email"/><br>
<input type="password" placeholder="Password" v-model="password"/><br>
<button v-on:click="login()">Login</button>
</div>
</template>
<script>
import firebase from 'firebase'
export default {
name: "Login",
data: function () {
return {
email: '',
password: ''
}
},
methods: {
login: function () {
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
this.$router.replace('home')
})
}
}
}
</script>
直到我输入用户名和密码并单击登录,firebase auth 成功并且控制台显示错误 this is undefined 之前它仍然可以。
我该如何解决这个错误?我无法使用路由器。
尝试使用箭头函数代替:
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
this.$router.replace('home')
})
login: function () {
let vm=this
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
vm.$router.replace('home')
})
}
带有 then
的回调函数没有可用的 login
函数的上下文。如果您能够使用 ES6,请改用箭头函数:
login: function () {
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
this.$router.replace('home')
})
}
但是,如果您出于某种原因无法使用 ES6,请尝试存储对 this
的引用并在函数内部使用它:
login: function () {
var self = this;
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
self.$router.replace('home')
})
}
使用箭头函数或bind()方法
示例:
foo().then(() => {
// access this
})
// or
foo().then(function() {
// access this
}.bind(this))
我已经创建了一个 vue 组件,如下所示:
<template>
<div class="login">
<h3>Sign in</h3>
<input type="text" placeholder="Email" v-model="email"/><br>
<input type="password" placeholder="Password" v-model="password"/><br>
<button v-on:click="login()">Login</button>
</div>
</template>
<script>
import firebase from 'firebase'
export default {
name: "Login",
data: function () {
return {
email: '',
password: ''
}
},
methods: {
login: function () {
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
this.$router.replace('home')
})
}
}
}
</script>
直到我输入用户名和密码并单击登录,firebase auth 成功并且控制台显示错误 this is undefined 之前它仍然可以。 我该如何解决这个错误?我无法使用路由器。
尝试使用箭头函数代替:
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
this.$router.replace('home')
})
login: function () {
let vm=this
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
vm.$router.replace('home')
})
}
带有 then
的回调函数没有可用的 login
函数的上下文。如果您能够使用 ES6,请改用箭头函数:
login: function () {
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then((user) => {
this.$router.replace('home')
})
}
但是,如果您出于某种原因无法使用 ES6,请尝试存储对 this
的引用并在函数内部使用它:
login: function () {
var self = this;
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(function (user) {
self.$router.replace('home')
})
}
使用箭头函数或bind()方法 示例:
foo().then(() => {
// access this
})
// or
foo().then(function() {
// access this
}.bind(this))