【问题标题】:'Maximum call stack size exceeded' VueJS routing'超出最大调用堆栈大小' VueJS 路由
【发布时间】:2019-09-04 21:30:27
【问题描述】:

基本上router.beforeEach() 方法正在做一些我不明白的事情。

我的问题是,当我的路由重定向到 /login 时,它会执行大约 960 次左右,直到发生错误。

我的代码是这样的:

路由器:

let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path:'/login',
      name: 'login',
      component: Login,
      meta: {
        requiresAuth: 'false'
      }
    },
    {
      path:'/register',
      name: 'register',
      component: Register,
      meta: {
        requiresAuth: 'false'
      }
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: {
        requiresAuth: 'True'
      }
    }
  ]
})

beforeEach() 方法

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {

    console.log(to.matched.some(record => record.meta.requiresAuth))
    if (localStorage.getItem('jwt') == null) {
      next({
       path: '/login',
        params: { nextUrl: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    if (localStorage.getItem('jwt') != null) {
      next({
        path: '/',
        params: { nextUrl: '/' }
      })
    } else {
      next()
    }
  }
})

我查看了无数线程和其他地方,没有一个和我有同样的问题(或者我忽略了一些事情)。任何人都知道如何修复,以及实际发生了什么导致错误发生?据我所知,我没有命名两次或任何其他功能/组件在不应该触发时触发。

【问题讨论】:

    标签: node.js vue.js routing vue-router


    【解决方案1】:

    修复它。我的脑袋有点特别。对于任何有同样问题的人,只需将路线更改为

    routes: [
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta: {
              requiresAuth: false
            }
        },
        {
          path:'/register',
          name: 'register',
          component: Register,
          meta: {
            requiresAuth: false
          }
        },
        {
          path: '/',
          name: 'home',
          component: Home,
          meta: {
            requiresAuth: true
          }
        }
      ]
    

    【讨论】:

    • 你能解释一下你做了什么吗?我没有看到任何变化?
    • @dexter,是的,基本上我在每条路由中应用的元数据我将 requiresAuth 的值设置为字符串而不是布尔值。并且 JS 在 ‘true’ == true 时返回 false。
    • 啊 tnx,现在我明白了!
    【解决方案2】:

    对于没有任何明显错误而出现此错误的任何人,请尝试删除 node_modules 并再次运行npm install。 我在切换 git 分支时遇到了这个错误,唯一改变的是包,所以我尝试了上面的方法,它解决了这个问题:)

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 2020-07-29
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2015-10-24
      • 2015-12-29
      相关资源
      最近更新 更多