在通过判断token的值来确定用户是不是已经登录,如果没有登录的用户只能访问登录页
router.beforeEach((to, from, next) => {
let token = window.localStorage.getItem('token');
if( !token){
next({path: '/'})
} else {
next()
}
})
以上这代码就是个死循环,因为没有出口,没有判断什么情况下不进行循环
所以只要确定当页面是登录页面的钩子就不让他循环下去
当跳转路径不是登录页并且token值不存在时才让他跳登录页,如果是登录就正常跳不要进入循环
router.beforeEach((to, from, next) => {
let token = window.localStorage.getItem('token');
debugger
if(to.path!='/' && !token){
next({path: '/'})
} else {
next()
}
})
还有就是当token存在时,用户输入登录页面也让他进入到首页,
当to.path是登