Vue Router warn The “next“ callback was called more than once in one navigation guard

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

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()删除

你可能感兴趣的:(Vue,vue.js,前端,vue-router)