blob: 7f638d2e33839100fa5bbce37d3a2b275c21caf6 [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001import Vue from 'vue'
2import Router from 'vue-router'
3
4// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
5// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading
6
7Vue.use(Router)
8
9/* Layout */
10import Layout from '../views/layout/Layout'
11import { getMenuList } from '../api/menulist-api'
12
13/**
14* hidden: true if `hidden:true` will not show in the sidebar(default is false)
15* alwaysShow: true if set true, will always show the root menu, whatever its child routes length
16* if not set alwaysShow, only more than one route under the children
17* it will becomes nested mode, otherwise not show the root menu
18* redirect: noredirect if `redirect:noredirect` will no redirct in the breadcrumb
19* name:'router-name' the name is used by <keep-alive> (must set!!!)
20* meta : {
21 title: 'title' the name show in submenu and breadcrumb (recommend set)
22 icon: 'svg-name' the icon show in the sidebar,
23 }
24**/
25// 全局路由
26const globalRouter = [
27 { path: '/login', component: () => import('@/views/login/index'), hidden: true },
28 { path: '/404', component: () => import('@/views/404'), hidden: true }
29]
30// 主页路由
31const mainRouter = {
32 path: '/',
33 component: Layout,
34 redirect: '/dashboard',
35 hidden: true,
36 children: [{
37 path: 'dashboard',
38 component: () => import('@/views/dashboard/index'),
39 name: 'Dashboard',
40 meta: { title: 'Dashboard', icon: 'dashboard', noCache: true }
41 }]
42}
43// 菜单路由
44const menuRouter = []
45
46const constantRouterMap = globalRouter.concat(mainRouter).concat(menuRouter)
47
48const router = new Router({
49 // mode: 'history', //后端支持可开
50 scrollBehavior: () => ({ y: 0 }),
51 hasLoadedDynamicRoutes: false, // 动态路由已加载
52 routes: constantRouterMap
53})
54
55router.beforeEach((to, from, next) => {
56 if (router.options.hasLoadedDynamicRoutes) {
57 next()
58 } else {
59 getMenuList().then(response => {
60 const menuList = initDynamicRouters(response.items)
61 router.options.routes.push(...menuList)
62 router.addRoutes(menuList)
63 router.addRoutes([{ path: '*', redirect: '/404', hidden: true }])
64 router.options.hasLoadedDynamicRoutes = true
65 next({ ...to, replace: true })
66 })
67 }
68})
69function getMenuResource(resource) {
70 let menuresource = resource
71 if (menuresource && menuresource.startsWith('/views/')) {
72 menuresource = menuresource.substring(6)
73 }
74 return menuresource
75}
76function getChildren(menus) {
77 const children = []
78 for (const menu of menus) {
79 const menuresource = getMenuResource(menu.resource)
80 const route = {
81 path: menu.path,
82 name: menu.code,
83 component: () => import('@/views' + menuresource),
84 meta: menu.meta,
85 children: menu.children ? getChildren(menu.children) : null
86 }
87 children.push(route)
88 }
89 return children.length > 0 ? children : null
90}
91
92function initDynamicRouters(menus) {
93 const menuList = []
94 if (menus && menus.length > 0) {
95 for (const menu of menus) {
96 const route = {
97 path: menu.path,
98 component: Layout,
99 // alwaysShow: true,
100 redirect: menu.path + '/' + ((menu.children && menu.children.length > 0) ? menu.children[0].path : null),
101 name: menu.code,
102 meta: menu.meta,
103 children: menu.children ? getChildren(menu.children) : null
104 }
105 menuList.push(route)
106 }
107 }
108 return menuList
109}
110
111export default router