node.js - “超出最大调用堆栈大小”VueJS 路由

标签 node.js vue.js routes vue-router

基本上,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()
    }
  }
})

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

最佳答案

已修复。我的头脑有点特殊。对于遇到同样问题的任何人,只需将路线更改为

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
      }
    }
  ]

关于node.js - “超出最大调用堆栈大小”VueJS 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55680043/

相关文章:

javascript - 在 SystemJS 加载器环境中将相对路径映射到绝对路径的方法

javascript - 如何使用 People API 和 Nodejs 创建 google 联系人

html - 使用 HTML 和 CSS 在剪辑路径上添加不透明度

vue.js - Vue.js 中所有导入的数组都必须是响应式的吗?

node.js - 在两个Docker容器之间(Vue.js到Node.js)与HTTP通信

node.js - 在应用程序启动时不启动 Node 服务器

javascript - 使用 npm 作为任务运行器/构建工具 - 某些 cli 模块有问题

javascript - 使用 Promise 链接 http 请求

ruby-on-rails - 为什么我在 "No route matches [GET]"请求上得到 "delete"? Rails 上的 Ruby

ios - 如何使用 skobbler iOS SDK 进行重新路由