登录 路由拦截

  • Cookie
router.beforeEach((to,from,next)=>{
  store.commit('setToken',Cookie.get('token'))
  if(store.state.token){
    store.commit('changIsSignIn',1)
  }
  if(to.meta.requireAuth){
    if(store.state.token){
      next()
    }else{
      next({path:'/login'})
    }
  }else{
    next()
  }
  
})
 
  • sessionStorage
 
//路由导航守卫
router.beforeEach((to,from,next)=>{
  const token=window.sessionStorage.getItem('token');
  if(to.path =='/login'){
    next();
  }else{
    if(token){
      next();
    }else{
      next('login');
    }
  }
})
posted @ 2021-06-15 16:25  zjxgdq  阅读(54)  评论(0编辑  收藏  举报