vue 路由控制权限登录

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


Vue.use(Router)

const Home = r => require.ensure([],() => r(require('@/components/Home')))
const personalInfomation=r=>require.ensure([],()=>r(require('@/pages/home/adminstrators/personalInfomation')))
const secretModify=r=>require.ensure([],()=>r(require('@/pages/home/adminstrators/secretModify')))
const visitRecord=r=>require.ensure([],()=>r(require('@/pages/home/adminstrators/visitRecord')))
const useStatic=r=>require.ensure([],()=>r(require('@/pages/home/adminstrators/useStatic')))
const loginDefault=r=>require.ensure([],()=>r(require('@/pages/auth/loginDefault')))
const loginPage=r=>require.ensure([],()=>r(require('@/pages/auth/login/login')))
const registered=r=>require.ensure([],()=>r(require('@/pages/auth/registered/registered')))

const definition=r=>require.ensure([],()=>r(require('@/pages/home/definition/definition')))
const lists=r=>require.ensure([],()=>r(require('@/pages/home/lists/lists')))
const result=r=>require.ensure([],()=>r(require('@/pages/home/result/result')))

const check=r=>require.ensure([],()=>r(require('@/pages/home/check/check')))
const tests=r=>require.ensure([],()=>r(require('@/pages/home/tests/tests')))

const surface=r=>require.ensure([],()=>r(require('@/pages/home/surface/surface')))
const operation=r=>require.ensure([],()=>r(require('@/pages/home/operation/operation')))
const database=r=>require.ensure([],()=>r(require('@/pages/home/database/database')))
const file=r=>require.ensure([],()=>r(require('@/pages/home/file/file')))
const safe=r=>require.ensure([],()=>r(require('@/pages/home/safe/safe')))
const statement=r=>require.ensure([],()=>r(require('@/pages/home/statement/statement')))
const port=r=>require.ensure([],()=>r(require('@/pages/home/port/port')))

const administrator=r=>require.ensure([],()=>r(require('@/pages/home/administrator/administrator')))
const record=r=>require.ensure([],()=>r(require('@/pages/home/record/record')))

const laas=r=>require.ensure([],()=>r(require('@/pages/home/laas/laas')))
const paas=r=>require.ensure([],()=>r(require('@/pages/home/paas/paas')))
const saas=r=>require.ensure([],()=>r(require('@/pages/home/saas/saas')))

var router = new Router({
  routes: [
    {
      path: '/loginDefault',
      name: 'loginDefault',
      meta: {
        requireAuth: true
      },
      component: loginDefault,
      children:[
        {
          path: '/login',
          name: 'loginPage',
          component: loginPage
        },
        {
          path: '/registered',
          name: 'registered',
          component: registered
        }
      ]
    },
    {
      path: '/',
      name: 'Home',
      meta: {
        requireAuth: true
      },
      component: Home,
      children:[
        {
          path:'personalInfomation',
          name:'personalInfomation',
          component:personalInfomation,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'secretModify',
          name:'secretModify',
          component:secretModify,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'visitRecord',
          name:'visitRecord',
          component:visitRecord,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'useStatic',
          name:'useStatic',
          component:useStatic,
          meta:{
            requireAuth:true
          }
        },,
        {
          path:'/definition',
          name:'definition',
          component:definition,
         
          meta:{
            requireAuth:true
          }
        },
        {
          path:'lists',
          name:'lists',
          component:lists,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'result',
          name:'result',
          component:result,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'check',
          name:'check',
          component:check,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'tests',
          name:'tests',
          component:tests,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'surface',
          name:'surface',
          component:surface,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'operation',
          name:'operation',
          component:operation,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'database',
          name:'database',
          component:database,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'file',
          name:'file',
          component:file,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'safe',
          name:'safe',
          component:safe,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'statement',
          name:'statement',
          component:statement,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'port',
          name:'port',
          component:port,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'administrator',
          name:'administrator',
          component:administrator,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'record',
          name:'record',
          component:record,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'laas',
          name:'laas',
          component:laas,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'saas',
          name:'saas',
          component:saas,
          meta:{
            requireAuth:true
          }
        },
        {
          path:'paas',
          name:'paas',
          component:paas,
          meta:{
            requireAuth:true
          }
        },
      ]
    }
  ]
})

// 配置路由权限
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (sessionStorage.getItem('token')) { // 判断本地是否存在access_token
      next()
    } else {
      // 未登录,跳转到登陆页面,并且带上 将要去的地址,方便登陆后跳转。
      next('/login')
    }
  } else {
    next()
  }
})

export default router

你可能感兴趣的:(vue)