gpt4 book ai didi

javascript - vue 最奇怪的行为 - 注释代码正在运行

转载 作者:行者123 更新时间:2023-11-30 19:03:39 25 4
gpt4 key购买 nike

我有一个以 firestore 作为数据库的 vue 项目。我曾经使用以下功能登录用户:

loginUser(){
if(this.email && this.password){
firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(() => {
this.$router.push({name: 'Index'})
}).catch(err => {
alert(err.message)
})
} else {
this.feedback = 'Please enter email & password.'
}
}

我编译并运行了这段代码,运行良好。然后根据控制台日志的建议,我更改了

import firebase from 'firebase'

import firebase from 'firebase/app'
import 'firebase/auth'

从那时起,我观察到一个最奇怪的行为。即使我注释掉代码this.$router.push({name: 'Index'}),它仍然会在登录时将我重定向到索引页面。我不知道该怎么做.虽然,最终我想在用户登录时将他们发送到索引页面,但如果代码被注释掉,它应该不会工作!我可能做错了什么?

Firebase 配置:

import firebase from 'firebase'

var firebaseConfig = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};
// Initialize Firebase
const firebaseApp = firebase.initializeApp(firebaseConfig);

export default firebaseApp.firestore()

路由器/index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '@/components/Index'
import Login from '@/components/Login'
import Signup from '@/components/Signup'
import firebase from 'firebase'

Vue.use(VueRouter)

const routes = [
{
path: '/',
name: 'Index',
component: Index,
meta: {
requiresAuth: true
}
},
{
path: '/login',
name: 'Login',
component: Login
},
{
path: '/register',
name: 'Signup',
component: Signup,
meta: {
requiresAuth: true
}
}
]

const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})

router.beforeEach((to, from, next) => {
if(to.matched.some(rec => rec.meta.requiresAuth)){
firebase.auth().onAuthStateChanged(user => {
if(user){
next()
} else {
next({name: 'Login'})
}
})
} else {
next()
}
})

export default router

我刚从从“firebase/app”导入 firebase从“firebase”导入 firebase

最佳答案

通过在 Firebase 配置文件中执行 export default firebaseApp.firestore(),您不会导出 Firebase Auth,而只会导出 Firestore。因此 firebase.auth().signInWithEmailAndPassword() 不起作用,您将被重定向到路由器中的默认页面/组件,即索引

按如下方式修改您的代码应该可以解决问题。

Firebase 配置

import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';

const firebaseConfig = {....};

firebase.initializeApp(firebaseConfig);

const db = firebase.firestore();
const auth = firebase.auth();


export { db, auth };

在 Vue.js 组件中

//...
<script>
const fb = require("../firebaseConfig.js"); //Adapt the path according to your files/folders structure
//....

methods: {
loginUser(){
if(this.email && this.password){
fb.auth.signInWithEmailAndPassword(this.email, this.password).then(() => {
this.$router.push({name: 'Index'})
}).catch(err => {
alert(err.message)
})
} else {
this.feedback = 'Please enter email & password.'
}
}
}
//....
</script>

关于javascript - vue 最奇怪的行为 - 注释代码正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224562/

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