问题:
在这里插入图片描述
问题代码:

router.beforeEach(async (to, from, next) => {
    const token = Cookies.get('token');
    next();
    if (to.path === '/login' || to.path === '/') {
        next();
    }
    else {
        if (token) {
            next();
        } else {
          console.log('pms out');
            next('/login');
        }
    }
})

问题点:
这个是路由导航next函数执行了两次,所以我们要检查下router.beforeEach

修改后的代码

router.beforeEach(async (to, from, next) => {
    const token = Cookies.get('token');
    if (to.path === '/login' || to.path === '/') {
        next();
    }
    else {
        if (token) {
            next();
        } else {
          console.log('pms out');
            next('/login');
        }
    }
})

就是把多余的next()删除

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐