2021-02-26 企业vue router 示例

 router/index.js

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Layout from '@/layout'

/* Router Modules 模块*/
import systemRouter from './module/system'
import gameRouter from './module/game'
import toolRuter from './module/tool'
import monitorRouter from './module/monitor'


// 公共路由
export const constantRoutes = [
 {
        path: '',
        component: Layout,
        redirect: 'index',
        children: [{
            path: 'index',
            component: (resolve) => require(['@/views/index'], resolve),
            name: 'Index',
            meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true }
        }]
    },
{
        path: '/login',
        component: (resolve) => require(['@/views/login'], resolve),
        hidden: true
    },
    {
        path: '/404',
        component: (resolve) => require(['@/views/error/404'], resolve),
        hidden: true
    },
{
        path: '/401',
        component: (resolve) => require(['@/views/error/401'], resolve),
        hidden: true
    },
  systemRouter,
    gameRouter,
    toolRuter,
    monitorRouter,

{
        path: '/user',
        component: Layout,
        hidden: true,
        redirect: 'noredirect',
        children: [{
            path: 'profile',
            component: (resolve) => require(['@/views/system/user/profile/index'], resolve),
            name: 'Profile',
            meta: { title: '个人中心', icon: 'user' }
        }]
    },


]

export default new Router({
    mode: 'history', // 去掉url中的#
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

模块:game.js 的路由信息


const gameRouter = {
    path: '/game',
    component: Layout,
    name: 'Game',
    hidden: true,
    meta: {
        title: '游戏平台',
        icon: 'system',
        noCache: false
    },
    children: [{
        path: 'game',
        component: () =>
            import('@/views/game/game/index'),
        name: 'Support',
        meta: { title: '平台支持游戏' }
    },
    {
        path: 'currency',
        component: () =>
            import('@/views/game/currency/index'),
        name: 'Currency',
        meta: { title: '游戏币' }
    },
    {
        path: 'goods',
        component: () =>
            import('@/views/game/goods/goods'),
        name: 'Goods',
        meta: { title: '平台商品' }
    },
    {
        path: 'chartroom',
        component: () =>
            import('@/views/game/chartroom/chartroom'),
        name: 'Chartroom',
        meta: { title: '聊天大厅' }
    },
    {
        path: 'kefu',
        component: () =>
            import('@/views/game/kefu/kefu'),
        name: 'Kefu',
        meta: { title: '客服配置' }
    },
    {
        path: 'race_schedule',
        component: () =>
            import('@/views/game/race_schedule/race_schedule'),
        name: 'RaceSchedule',
        meta: { title: '赛程管理' }
    },
    {
        path: 'race_type',
        component: () =>
            import('@/views/game/race_type/race_type'),
        name: 'RaceType',
        meta: { title: '赛事种类' }
    },
    // hot_problem
    {
        path: 'hot_problem',
        component: () =>
            import('@/views/game/hot_problem/hot_problem'),
        name: 'HotProblem',
        meta: { title: '热门问题' }
    },
    // settlement_rule
    {
        path: 'settlement_rule',
        component: () =>
            import('@/views/game/settlement_rule/settlement_rule'),
        name: 'SettlementRule',
        meta: { title: '结算规则' }
    },
    {
        path: 'player',
        component: () =>
            import('@/views/game/player/player'),
        name: 'Player',
        meta: { title: '玩家管理' },
    },
    {
        path: 'coin_robot',
        component: () =>
            import('@/views/game/coin_robot/coin_robot'),
        name: 'CoinRobot',
        meta: { title: '收币机器人' },
    },
    // Invite_rewards
    {
        path: 'Invite_rewards',
        component: () =>
            import('@/views/game/Invite_rewards/Invite_rewards'),
        name: 'InviteRewards',
        meta: { title: '邀请奖励' },
    },
    {
        path: 'notice_template',
        component: () =>
            import('@/views/game/notice_template/notice_template'),
        name: 'NoticeTemplate',
        meta: { title: '消息模板' },
    },

    // notice
    {
        path: 'notice',
        component: () =>
            import('@/views/game/notice/notice'),
        name: 'Notice',
        meta: { title: '平台消息通知' },
    },
    {
        path:'account',
        component: () =>
        import('@/views/game/account/index'),

        children: [{
            path: 'ticket',
            component: () =>
                import('@/views/game/account/ticket'),
            name: 'Ticket',
            meta: { title: '玩家门票' }
        },{
            path: 'currency',
            component: () =>
                import('@/views/game/account/currency'),
            name: 'Currency',
            meta: { title: '玩家游戏币' }
        },]

    },
    {
        path: 'chat_list',
        component: () =>
            import('@/views/game/chat_list/chat_list'),
        name: 'chatList',
        meta: { title: '消息列表' },
    },


    ]
}

export default gameRouter;

permission.js 

import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect', '/bind', '/register']

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    /* has token*/
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => {
          // 拉取user_info
          const roles = res.roles
          store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
            // 根据roles权限生成可访问的路由表
            router.addRoutes(accessRoutes) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
          })
        }).catch(err => {
          store.dispatch('LogOut').then(() => {
            Message.error(err)
            next({ path: '/' })
          })
        })
      } else {
        next()
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单,直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

main.js

import './permission' 

上一篇:django路由url


下一篇:Django框架之URL与视图详解(你想要的这都有!!!)