vue+axios路由拦截

const router = new Router({
  routes: [
    {path: '/', redirect: '/login'},
    {path: '/login', name: 'login', component: login},
    {
      path: '/manage', name: 'manage', component: manage,

      children: [
        {path: '/manage/', redirect: '/manage/index'},
        {path: '/manage/index', name: 'index', component: Index, meta: {requireAuth: true}},
        {path: '/manage/travelOrder', name: 'travelOrder', component: travelOrder, meta: {requireAuth: true}}
      ]
    },

  ]
});
    
// meta: {
//   requireAuth: true,  // 添加该字段,表示进入这个路由是需要登录的
// },
router.beforeEach((to, from, next) => {

  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
   // setTimeout(()=>{
      if (localStorage.getItem('token')) {  // localStorage获取当前的token是否存在
        next();
      }
      else {
        Message.error('请重新登陆');
        next({
          path: '/login',
          query: {redirect: to.fullPath}  // 将跳转的路由path作为参数,登录成功后跳转到该路由
        })
      }
   // },10000)
  }
  else {
    next();
  }
})

你可能感兴趣的:(vue)