Vue路由守卫

router.beforeEach((to, from, next) => {
 let user_name = localStorage.getItem('user_name')
 if (to.path === '/') {
  next()
  return
 } else {
  if (user_name === null || user_name === '') {
   alert('您暂未登录,请先登录')
   next('/')
  } else {
   next()
  }
 }
})
原文地址:https://www.cnblogs.com/lyt520/p/14663451.html