Vuejs2 Vuejs路由器和每个挂钩之前
我在vue router的beforeEach钩子上遇到了一个问题:我在main.js中检查了用户与路由器的身份验证。beforeEach钩子如下:Vuejs2 Vuejs路由器和每个挂钩之前,vuejs2,vue-router,Vuejs2,Vue Router,我在vue router的beforeEach钩子上遇到了一个问题:我在main.js中检查了用户与路由器的身份验证。beforeEach钩子如下: router.beforeEach((to, from, next) => { if (to.name !== 'Login' && to.name !== 'PasswordReset' && to.name !== 'ForgetPass' && to.name !== 'SsoLogi
router.beforeEach((to, from, next) => {
if (to.name !== 'Login' && to.name !== 'PasswordReset' && to.name !== 'ForgetPass' && to.name !== 'SsoLogin') {
Auth.isLogged()
.then(() => {
next();
})
.catch(() => {
next(`/login?redirectUrl=${to.fullPath}`);
});
}
next();
});
问题是网页在重定向发生之前会显示一秒钟。
我试着把上面的代码放在Vue之前。像我在互联网上看到的那样使用(路由器),但它没有修复任何问题。
我可以通过在每个路由上使用beforeEnter()来修复它,但我希望使用全局解决方案,而不必在每个路由上增加代码
你能帮我吗
router.beforeEach((to, from, next) => {
if(...) {
Auth.isLogged().then().catch();
} else {
next()
}
}
您需要添加else,因为Auth.islogge()是异步的。如果不添加else,将始终执行最后一个next()。因此,您将首先看到页面(执行最后一个next())
,然后看到页面重定向(执行了then()或catch()块中的代码)