我正在为一些url创建一个守卫。我在用
beforeEach
router.beforeEach((to, from, next) => {
var isLoggedIn = localStorage.getItem('cacheTokenId');
if(to.meta.guestOnly && isLoggedIn) {
next({ name: 'cars' })
} else if(to.meta.authOnly && !isLoggedIn) {
console.log("authOnly")
next({ name: 'login' })
}
next()
})
我的问题是最后一个
next
(外部条件)总是被执行。我想
next()
在调用时停止执行。
现在,我的解决方案已经走到了最后
下一步()
在一个
else
if(to.meta.guestOnly && isLoggedIn) {
console.log("guestOnly")
next({ name: 'cars' })
} else if(to.meta.authOnly && !isLoggedIn) {
console.log("authOnly")
next({ name: 'login' })
} else {
next()
}
为什么这是必要的,我错过了什么吗?