Firebase beforeeach()触发很多次(Vue应用)

时间:2018-11-14 12:17:24

标签: javascript firebase vue.js firebase-authentication vue-router

我正在使用Firebase和Vue js构建帖子应用程序。我有一些问题: 我的路由器:

import Vue from 'vue'
import Router from 'vue-router'
import Dashboard from '@/components/Dashboard'
import Login from '@/components/Login'
import SignUp from '@/components/SingUp'
import RecentPosts from '@/components/RecentPosts';
import firebase from 'firebase'

Vue.use(Router)

let router = new Router({
  routes: [
   ...... some routes
    {
      path: '/recentPosts',
      name: 'RecentPosts',
      component: RecentPosts,
      meta:{
        requireAuth: true
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  let currentUser = firebase.auth().currentUser;
  let requiresAuth = to.matched.some(r => r.meta.requiresAuth);
  console.log("CURRENTUSER",currentUser);
  if(!currentUser && requiresAuth) {
    next('Login');
  }else if(currentUser && !requiresAuth) {
    next('Dashboard');
  }else if (currentUser && requiresAuth) {
    next();
  }else {
    next();
  }
});
export default router;

在我转到/recentPosts之后,我的函数会被每次触发很多次,以至于阻止了我的应用程序。 为什么我无法访问/ recentPosts?我已登录用户,因此它应该可以解决我的溃败,但事实并非如此。

else if (currentUser && requiresAuth) {
    next();
}

它应该解决我的'/ recentsposts'路线,但不是

1 个答案:

答案 0 :(得分:2)

这可能是因为您在require(s)处有错字:

    {
      path: '/recentPosts',
      name: 'RecentPosts',
      component: RecentPosts,
      meta:{
        requireAuth: true     // <- without "s" at the end of require
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  let currentUser = firebase.auth().currentUser;
  let requiresAuth = to.matched.some(r => r.meta.requiresAuth);   // <- with an "s" at the end of requires