vue路由器引起internalerror:递归太多
我有以下vue路由器文件,我希望路由器将用户重新用于寄存器页面,以防他未登录。错误:
InternalError: too much recursion
控制台输出也未登录,这意味着该错误是在IF语句主体中引起的下一个方法。
路由器索引
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)
const routes = [
{
path: '/',
redirect: 'Dashboard',
},
{
path: '/dashboard',
name: 'dashboard',
props: true,
component: () => import('@/views/dashboard/Dashboard.vue'),
},
{
path: '/Dashboard',
name: 'Dashboard',
props: true,
component: () => import('@/views/dashboard/Dashboard.vue'),
},
{
path: '/ProjectPage/:id',
name: 'ProjectPage',
props: true,
component: () => import('@/views/pages/ProjectPage/ProjectPage.vue')
},
{
path: '/MyIssues',
name: 'MyIssues',
props: true,
component: () => import('@/views/pages/MyIssuesPage/MyIssuesPage.vue')
},
{
path: '/IssuePage/:id',
name: 'IssuePage',
props: true,
component: () => import('@/views/pages/IssuePage/IssuePage.vue')
},
{
path: '/CreateProject',
name: 'CreateProject',
component: () => import('@/views/pages/CreateProjectPage/CreateProject.vue')
},
{
path: '/pages/account-settings',
name: 'pages-account-settings',
component: () => import('@/views/pages/account-settings/AccountSettings.vue'),
},
{
path: '/pages/login',
name: 'pages-login',
component: () => import('@/views/pages/Login.vue'),
meta: {
layout: 'blank',
requireLogin: false,
},
},
{
path: '/pages/register',
name: 'pages-register',
component: () => import('@/views/pages/Register.vue'),
meta: {
layout: 'blank',
requireLogin: false,
},
},
{
path: '/error-404',
name: 'error-404',
component: () => import('@/views/Error.vue'),
meta: {
layout: 'blank',
},
},
{
path: '*',
redirect: 'error-404',
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
})
router.beforeEach((to, from, next) => {
if (to.matched.some(record => !record.meta.requireLogin) && !store.state.isAuthenticated) {
console.log('not logged in')
next({ name: 'pages-register', query: { to: to.path } });
} else {
console.log('logged in')
next()
}
});
export default router
I have the following vue router file, and I want the router to rediret the user to the register page in case he is not logged in. When the user is logged in, it works fine, but when he is not, I get the following error:
InternalError: too much recursion
The console outputs not logged in also which means that the error is caused the next method in the if statement body.
Router index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)
const routes = [
{
path: '/',
redirect: 'Dashboard',
},
{
path: '/dashboard',
name: 'dashboard',
props: true,
component: () => import('@/views/dashboard/Dashboard.vue'),
},
{
path: '/Dashboard',
name: 'Dashboard',
props: true,
component: () => import('@/views/dashboard/Dashboard.vue'),
},
{
path: '/ProjectPage/:id',
name: 'ProjectPage',
props: true,
component: () => import('@/views/pages/ProjectPage/ProjectPage.vue')
},
{
path: '/MyIssues',
name: 'MyIssues',
props: true,
component: () => import('@/views/pages/MyIssuesPage/MyIssuesPage.vue')
},
{
path: '/IssuePage/:id',
name: 'IssuePage',
props: true,
component: () => import('@/views/pages/IssuePage/IssuePage.vue')
},
{
path: '/CreateProject',
name: 'CreateProject',
component: () => import('@/views/pages/CreateProjectPage/CreateProject.vue')
},
{
path: '/pages/account-settings',
name: 'pages-account-settings',
component: () => import('@/views/pages/account-settings/AccountSettings.vue'),
},
{
path: '/pages/login',
name: 'pages-login',
component: () => import('@/views/pages/Login.vue'),
meta: {
layout: 'blank',
requireLogin: false,
},
},
{
path: '/pages/register',
name: 'pages-register',
component: () => import('@/views/pages/Register.vue'),
meta: {
layout: 'blank',
requireLogin: false,
},
},
{
path: '/error-404',
name: 'error-404',
component: () => import('@/views/Error.vue'),
meta: {
layout: 'blank',
},
},
{
path: '*',
redirect: 'error-404',
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
})
router.beforeEach((to, from, next) => {
if (to.matched.some(record => !record.meta.requireLogin) && !store.state.isAuthenticated) {
console.log('not logged in')
next({ name: 'pages-register', query: { to: to.path } });
} else {
console.log('logged in')
next()
}
});
export default router
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pages-register
也受到保护。您必须删除它。否则,您将遇到递归路由
The
pages-register
is also guarded.You must remove it. Otherwise, You will encounter recursive routing