{if(EN"> How to solve Vue Router infinite redirect error-PHP Chinese Network Q&A
How to solve Vue Router infinite redirect error
P粉403821740
P粉403821740 2023-08-25 16:12:12
0
1
835

In router.beforeEach, I want to check if the sessionToken already exists in the storage and if not redirect to the login page to get it, but I get the following error:

Navigation guard detected infinite redirect when jumping from "/" to "/login". To avoid stack overflow, abort navigation. If not fixed, this will cause problems in production environments. 

Code in my router.js:

router.beforeEach((to, from, next) => { if(ENV == 'development') { let sessionStorage = storage.sessionStorageGet('_sessionToken') if (sessionStorage === null) next({ name: 'Login' }) else next() } }) 
P粉403821740
P粉403821740

reply all (1)
P粉529245050
const routes = [ { path: '/login', name: 'Login', component: () => import('../views/login'), meta: { requiresAuth: false } }, { path: '/private', ... private route config, meta: { requiresAuth: true } } ]; router.beforeEach(async (to, from, next) => { if (ENV == 'development') { if (to.matched.some(record => record.meta.requiresAuth)) { const sessionStorage = storage.sessionStorageGet('_sessionToken') if (sessionStorage) { next(); } else { router.push({ name: 'Login' }); } } else { next(); } } });
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!