vue使用router.beforeEach()

router.beforeEach()一般用来做一些进入页面的限制。比如没有登录,就不能进入某些页面,只有登录了之后才有权限查看某些页面。。。说白了就是路由拦截。

1、我们可以在路由中做以下配置,表示当前路由下的页面需要登录权限

meta:{requireAuth:true//true为这个页面需要登录权限 }

 2、在main.js中写上路由拦截

router.beforeEach((to,from,next)=>{
  //从cookie中取值并给vux中的token赋值 store.commit('setToken',Cookie.get('token'))
  //如果vux有值就讲登录状态改成1 if(store.state.token){ store.commit('changIsSignIn',1) }
 //判断是否为true,如果为true就是要登录,去判断token是否存在。存在就执行,不存在就跳转登录页面 if(to.meta.requireAuth){ if(store.state.token){ next() }else{ next({path:'/login'}) } }else{ next() } })

  

猜你喜欢

转载自www.cnblogs.com/deng-jie/p/12792573.html