死循环版(错误示例):
router.beforeEach((to, from, next) => { if(localStorage.getItem("token-xsm") != null) { next(); }else { next('/login') } })
正确版本:
router.beforeEach((to, from, next) => { if(localStorage.getItem("token-xsm") != null) { next(); }else { if(to.path == '/login'){ next() } else{ next('/login') } } })
总结: 首先要明白beforeEach该函数的运行机制是每次访问路由跳转时都要执行该钩函数 当token不存在时 跳转到登录页面,