gpt4 book ai didi

javascript - 无法读取未定义的属性 '$router'?

转载 作者:行者123 更新时间:2023-12-02 23:57:27 25 4
gpt4 key购买 nike

我正在尝试让用户可以登录页面,但向我显示此消息“无法读取未定义的属性'$router'”

我在 Vue 中登录的代码,它位于导出默认值内

import {fb} from "../firebase";
import router from '../router';

export default {
name: "NavBar",
components: {...},
data() {
return {
login: false,
register: false,
name: null,
email: null,
pwd: null,
};
},
methods: {
logInner(){
fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
.then(function(){
this.$router.replace('users');
})
.catch(function(error) {`enter code here`
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
});

},
registering() {
fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
.then((user) =>{
this.$router.replace('users');
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == "auth/weak-password") {
alert("The password is too weak.");
} else {
alert(errorMessage);
}
console.log(error);
});
}
}
};

我尝试过 router.push,甚至 router.go 也没有帮助。有人可以帮我吗?

最佳答案

this 在 fb.auth() 函数中不可用,因此请尝试以下操作。有关详细说明,请检查此答案:https://stackoverflow.com/a/47148828/9489397

logInner(){
let self = this
fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
.then(function(){
self.$router.replace('users');
})

registering() {
let self = this
fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
.then((user) =>{
self.$router.replace('users');
})

关于javascript - 无法读取未定义的属性 '$router'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com