ZSWYD

简单导航守卫使用

需要用户登录的页面配置 meta:{requiresAuth:true},

VueRouter.beforeEach((to, from, next) => {
  //获取登录状态
  let isLogin = localStorage.getItem('isLogin')
  //判断要去往的页面有没有设置meta
  if(to.matched.some(rec => rec.meta.requiresAuth)){    
    //判断用户是否登录
    if(!isLogin){
      Message({
        type:'error',
        message:'您还没有登录,请登录后再进入该页面'
      })
      next('/home')
    }else{
      //登录了
      next()
    }
  }else{
    //没有设置meta,不需要登录直接放行
    next()
  }
})

posted on 2022-09-14 12:01  苏舒  阅读(27)  评论(0)    收藏  举报

导航