vue-router.esm.js?fe87:16 [vue-router] uncaught error during route navigation:

发生错误的代码:

routerMap.beforeEach((to,from,next) => {
    const token = Cookies.get("token");
    if(!token){//未登录,强制登录
        next({
            name:"login"  // 将跳转的路由path作为参数,登录成功后跳转到该路由
        });
    }else{
        next();
    }
});

错误截图:

router.beforeeach Maximum call stack size exceeded
vue-router.esm.js?fe87:16 [vue-router] uncaught error during route navigation:_第1张图片

解决方案:
根据报错信息,得知:这段代码进入了dead loop。
To prevent the dead loop, just add a check :

routerMap.beforeEach((to,from,next) => {
    const token = Cookies.get("token");
    if(!token && to.path != '/login'){//未登录,强制登录
        next({
            name:"login"  // 将跳转的路由path作为参数,登录成功后跳转到该路由
        });
    }else{
        next();
    }
});

你可能感兴趣的:(vue)