vue-router封裝

用戶是否須要登陸vue

import Vue from 'vue'
import Router from 'vue-router'vue-router

Vue.use(Router)
const routes = [{
  path: '/',
  redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () =>import('./views/Home.vue'),
    meta: {
      requireAuth: true, // 添加該字段,表示進入這個路由是須要登陸的
    },
    children: [{
      path: '/account_management',
      name: 'account_management',
      component: () =>import('./views/admin/account_management.vue')
    }]ui

  },
  {
    path: '/login',
    name: 'login',
    component: () =>
    import('./views/login/login.vue')
  }

]
const router = new Router({
  routes
})
//路由攔截器
router.beforeEach((to, from, next) => {
  //根據字段判斷是否路由過濾
  if(to.meta.requireAuth) { // 判斷該路由是否須要登陸權限
    if(localStorage.getItem('sid')) { //身份信息獲取
      next();
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next();
  }
});
export default router;component

相關文章
相關標籤/搜索