router.beforeEach死循环问题
1,router使用了beforeEach,项目中如果未登录,需要直接跳转到登录页面,由于没有做判断,直接 next(’/login’),这样会导致路由死循环,所以在这里做判断后直接next();
2,下面代码使用了三目运算符,如果要跳转的路由是login的路由,就直接执行next()
router.beforeEach((to, from, next) => {
const isLogin = sessionStorage.getItem('loginData')
if (isLogin) {
next()
} else {
//跳出循环
to.path === '/login' ? next() : next('/login');
}
})