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

标签 javascript firebase vuejs2 firebase-authentication

我有一个以 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/

相关文章:

javascript - 如何使用 Three.js r71 合并两个几何图形或网格?

javascript - 非 jQuery 相当于 :visible in JavaScript?

javascript - 不同类别图像的下一个和上一个按钮

android - 如何使用 FirebaseAuth 从 Google 提供商处获取性别和生日?

vue.js - 更改 Vuetify 轮播高度

javascript - d3js : How to pass button id to onclick callback function

android - 是否可以在单个项目/应用程序中为发布和 Debug模式制作不同的 Firebase 数据库?

swift - 数据未显示在 Firebase 的 TableView 中

vue.js - VueJS 在点击时添加删除类

javascript - 我在使用 VueJS 将数据输出到动态生成的表时遇到问题