我想重定向用户到主页,如果登录,并希望访问登录/注册页面,并重定向用户到登录页面,如果没有登录,并希望访问其他页面.有些页面被排除在外,这意味着不需要登录,因此我的代码如下所示:
router.beforeEach((to, from, next) => {
if(
to.name == 'About' ||
to.name == 'ContactUs' ||
to.name == 'Terms'
)
{
next();
}
else
{
axios.get('/already-loggedin')
.then(response => {
// response.data is true or false
if(response.data)
{
if(to.name == 'Login' || to.name == 'Register')
{
next({name: 'Home'});
}
else
{
next();
}
}
else
{
next({name: 'Login'});
}
})
.catch(error => {
// console.log(error);
});
}
});
但问题是,它进入了一个无限循环,例如,每次登录页面加载且用户未登录时,它都会将用户重定向到登录页面,然后再次重定向到登录页面,然后...
我该怎么解决这个问题?