【问题标题】:Infinite loop with vue router beforeEach and children paths带有 vue 路由器 beforeEach 和子路径的无限循环
【发布时间】:2019-11-03 09:52:39
【问题描述】:

当我将 beforeEach 与子路径一起使用时,调试控制台显示此错误:vue-router.esm.js?8c4f:2079 RangeError: Maximum call stack size exceeded

import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginMixin from '@/mixins/LoginMixin.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: require('@/views/Home.vue').default,
  },
  {
    path: '/login',
    name: 'login',
    meta: { layout: 'centered' },
    component: () => import('@/views/Login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta: { layout: 'centered' },
    component: () => import('@/views/Register.vue'),
    children: [
      {
        path: 'user',
        component: () => import('@/components/RegisterForm.vue'),
      },
      {
        path: 'company',
        component: () => import('@/components/CompanyForm.vue'),
      }
    ]
  },
]

//creamos la instancia router modo history(urls amigables)
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path != '/login' || to.path != '/register/user' && LoginMixin.methods.loginMixinCheckAuth() == false) {
    //if not logead and join to other page distinc of login or register redirect to login
    next('/login')
  }
  else {
    next()
  }
})

我不知道什么是坏的,语法很好,函数 LoginMixin.methods.loginMixinCheckAuth() 运行良好(我在没有函数的情况下测试,结果是一样的)。

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

    嗯,乍一看,我会尝试在您的 beforeEach 方法中简化这个复杂的 if。尝试将requiresAuth: true 之类的内容添加到需要登录用户的所有路由的元数据中。

    从某种意义上说,您希望在您的路线中使用这样的东西:

      // ...
      {
        path: '/users/:userId(\\d+)/edit/',
        name: 'EditUser'
        props: true,
        meta: {
            requiresAuth: true, // <-- add this meta flag against which you check later in beforeEach
        },
        component: () => import(/* webpackChunkName: "user-edit" */ '@/views/UserEdit.vue'),
      },
      // ...
    

    这在你的beforeEach:

    router.beforeEach((to, from, next) => {
        if (to.matched.some((record) => record.meta.requiresAuth)) { // <-- check for requiresAuth here
            // assuming your login mixin works
            // if I were you I'd store some JWT in localStorage and check that somehow in a vuex getter
            if (!LoginMixin.methods.loginMixinCheckAuth()) {
                next('/login')
            } else {
                next()
            }
        } else {
            next()
        }
    })
    

    要完整回答这个问题会有点笨重,所以去看看我是如何使用metahere 并实现了beforeEachrule here

    【讨论】:

    • 我只尝试了 to.path != '/login' 并且工作正常,当我检查是否有子路径时出现问题
    • 嗯,那么您必须向我们展示您的 Register 及其所有子组件。如何在Register.vue 中包含&lt;router-view /&gt;?看看我是如何在上面链接的GitHub repo 中做到的。
    • 注册视图有
    • 我在每条路径上都使用 beforeEnter 修复了这个问题,但我的代码现在是意大利面条:(
    • 哎呀,不要那样做。 :/ 使用一种更简洁的方法,就像我链接到的那样。
    猜你喜欢
    • 2019-05-02
    • 2019-02-22
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2019-08-11
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多