【发布时间】:2019-08-18 03:59:51
【问题描述】:
我有以下代码:
router.beforeEach((to, from, next) => {
if (to.name !== from.name) {
store
.dispatch("fetchCurrentUser")
.then(() => {
console.log('then');
// do something
next();
})
.catch(() => {
console.log('catch');
router.push("/login");
next();
});
} else {
next();
}
// next();
});
我正在尝试获取当前用户,如果成功,则对这些数据进行处理,如果请求不成功,则将用户重定向到登录页面。但是 next () 调用不起作用,我在控制台中得到了“then”或“catch”,但是没有发生重定向,并且开始了无限循环。但是,如果我从条件(注释行)中获取 next (),则重定向工作正常。
【问题讨论】:
-
您还有其他路线守卫吗?用
beforeResolve替换beforeEach会改变什么吗? -
@raina77ow,不,beforeResolve 的行为完全相同
-
你能提供你的
fetchCurrentUser的代码吗?我认为错误可能隐藏在这里。
标签: vue.js vue-router