基本上,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/