文档https://panjiachen.github.io/vue-element-admin-site/zh/guide/#%E5%8A%9F%E8%83%BD
分析https://juejin.cn/post/6844903476661583880#heading-8

克隆安装

git克隆项目

git clone https://github.com/PanJiaChen/vue-element-admin.git

安装依赖

mpn i
遇到报错:tui-editor里面引用了tui-chart tui-chart依赖Raphael是GitHub链接,删除tui-editor后安装依赖正常

Vue.use(Router)

/ Layout / import Layout from ‘@/layout’

/ Router Modules / import componentsRouter from ‘./modules/components’ import chartsRouter from ‘./modules/charts’ import tableRouter from ‘./modules/table’ import nestedRouter from ‘./modules/nested’

/**

  • Note: sub-menu only appear when route children.length >= 1
  • Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html *
  • hidden: true if set true, item will not show in the sidebar(default is false)
  • alwaysShow: true if set true, will always show the root menu
  • if not set alwaysShow, when item has more than one children route,
  • it will becomes nested mode, otherwise not show the root menu
  • redirect: noRedirect if set noRedirect will no redirect in the breadcrumb
  • name:’router-name’ the name is used by (must set!!!)
  • meta : { roles: [‘admin’,’editor’] control the page roles (you can set multiple roles) title: ‘title’ the name show in sidebar and breadcrumb (recommend set) icon: ‘svg-name’/‘el-icon-x’ the icon show in the sidebar noCache: true if set true, the page will no be cached(default is false) affix: true if set true, the tag will affix in the tags-view breadcrumb: false if set false, the item will hidden in breadcrumb(default is true) activeMenu: ‘/example/list’ if set path, the sidebar will highlight the path you set } */

/**

  • constantRoutes
  • a base page that does not have permission requirements
  • all roles can be accessed */ export const constantRoutes = [ { path: ‘/profile’, component: Layout, redirect: ‘/profile/index’, hidden: true, children: [ {
    1. path: 'index',
    2. component: () => import('@/views/profile/index'),
    3. name: 'Profile',
    4. meta: { title: 'Profile', icon: 'user', noCache: true }
    } ] }, { path: ‘/404’, component: () => import(‘@/views/error-page/404’), hidden: true }, { path: ‘/401’, component: () => import(‘@/views/error-page/401’), hidden: true }, 省略省略省略省略省略省略省略省略省略省略 ]

/**

  • asyncRoutes
  • the routes that need to be dynamically loaded based on user roles */ export const asyncRoutes = [ 省略省略省略省略省略省略省略省略省略省略省略 { path: ‘external-link’, component: Layout, children: [ {
    1. path: 'https://github.com/PanJiaChen/vue-element-admin',
    2. meta: { title: 'External Link', icon: 'link' }
    } ] }, 最后最后最后最后最后最后最后最后最后最后最后最后最后 // 404 page must be placed at the end !!! { path: ‘*’, redirect: ‘/404’, hidden: true } ]

const createRouter = () => new Router({ // mode: ‘history’, // require service support scrollBehavior: () => ({ y: 0 }), routes: constantRoutes })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 export function resetRouter() { const newRouter = createRouter() router.matcher = newRouter.matcher // reset router }

export default router

  1. **注意事项**<br />**如果这里有一个需要非常注意的地方就是 404 页面一定要最后加载,如果放在 constantRoutes 一同声明了 404 ,后面的所有页面都会被拦截到404**
  2. - 注册路由操作
  3. ```javascript
  4. • const createRouter = () => new Router({
  5. // mode: 'history', // require service support
  6. scrollBehavior: () => ({ y: 0 }),
  7. routes: constantRoutes
  8. })
  9. const router = createRouter()
  10. 类似与const route = newRouter({})
  • 为什么只注册constantRoutes没有注册asyncRoutes 目前不清楚?? ```javascript export function resetRouter() { const newRouter = createRouter() router.matcher = newRouter.matcher // reset router }
  1. **路由划分**<br />这里的路由分为两种,`constantRoutes` `asyncRoutes`。<br />**constantRoutes:** 代表那些不需要动态判断权限的路由,如登录页、404、等通用页面。<br />**asyncRoutes:** 代表那些需求动态判断权限并通过 `addRoutes` 动态添加的页面。
  2. <a name="FGAJ0"></a>
  3. ### 示例
  4. <a name="Ox9oj"></a>
  5. ## constantRoutes
  6. - 需要侧边栏Layout的都使用路由嵌套,不使用的配置 `hidden: false`
  7. - @是配置的,@是src绝对路径
  8. - ` component: () => import('@/views/redirect/index')`路由懒加载,不使用不引入
  9. - **:path(.*)??**是什么,貌似是占位
  10. ```javascript
  11. export const constantRoutes = [
  12. {
  13. path: '/redirect',
  14. component: Layout,
  15. hidden: true,
  16. children: [
  17. {
  18. path: '/redirect/:path(.*)',
  19. component: () => import('@/views/redirect/index')
  20. }
  21. ]
  22. },
  23. {
  24. path: '/login',
  25. component: () => import('@/views/login/index'),
  26. hidden: true
  27. },
  28. {
  29. path: '/auth-redirect',
  30. component: () => import('@/views/login/auth-redirect'),
  31. hidden: true
  32. },
  33. {
  34. path: '/404',
  35. component: () => import('@/views/error-page/404'),
  36. hidden: true
  37. },
  38. {
  39. path: '/401',
  40. component: () => import('@/views/error-page/401'),
  41. hidden: true
  42. },
  43. {
  44. path: '/',
  45. component: Layout,
  46. redirect: '/dashboard',
  47. children: [
  48. {
  49. path: 'dashboard',
  50. component: () => import('@/views/dashboard/index'),
  51. name: 'Dashboard',
  52. meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
  53. }
  54. ]
  55. },

asyncRoutes

  • 需要侧边栏Layout的都使用路由嵌套,不使用的配置 hidden: false
  • @是配置的,@是src绝对路径
  • component: () => import('@/views/redirect/index')路由懒加载,不使用不引入
  • 比较长复杂的路由,可以划分出来,再单独引入
  • image.png

划分出去的其中一个

  1. /** When your routing table is too long, you can split it into small modules **/
  2. import Layout from '@/layout'
  3. const nestedRouter = {
  4. path: '/nested',
  5. component: Layout,
  6. redirect: '/nested/menu1/menu1-1',
  7. name: 'Nested',
  8. meta: {
  9. title: 'Nested Routes',
  10. icon: 'nested'
  11. },
  12. children: [
  13. {
  14. path: 'menu1',
  15. component: () => import('@/views/nested/menu1/index'), // Parent router-view
  16. name: 'Menu1',
  17. meta: { title: 'Menu 1' },
  18. redirect: '/nested/menu1/menu1-1',
  19. children: [
  20. {
  21. path: 'menu1-1',
  22. component: () => import('@/views/nested/menu1/menu1-1'),
  23. name: 'Menu1-1',
  24. meta: { title: 'Menu 1-1' }
  25. },
  26. {
  27. path: 'menu1-2',
  28. component: () => import('@/views/nested/menu1/menu1-2'),
  29. name: 'Menu1-2',
  30. redirect: '/nested/menu1/menu1-2/menu1-2-1',
  31. meta: { title: 'Menu 1-2' },
  32. children: [
  33. {
  34. path: 'menu1-2-1',
  35. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
  36. name: 'Menu1-2-1',
  37. meta: { title: 'Menu 1-2-1' }
  38. },
  39. {
  40. path: 'menu1-2-2',
  41. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
  42. name: 'Menu1-2-2',
  43. meta: { title: 'Menu 1-2-2' }
  44. }
  45. ]
  46. },
  47. {
  48. path: 'menu1-3',
  49. component: () => import('@/views/nested/menu1/menu1-3'),
  50. name: 'Menu1-3',
  51. meta: { title: 'Menu 1-3' }
  52. }
  53. ]
  54. },
  55. {
  56. path: 'menu2',
  57. name: 'Menu2',
  58. component: () => import('@/views/nested/menu2/index'),
  59. meta: { title: 'Menu 2' }
  60. }
  61. ]
  62. }
  63. export default nestedRouter

完整示例

  1. import componentsRouter from './modules/components'
  2. import chartsRouter from './modules/charts'
  3. export const asyncRoutes = [
  4. {
  5. path: '/permission',
  6. component: Layout,
  7. redirect: '/permission/page',
  8. alwaysShow: true, // will always show the root menu
  9. name: 'Permission',
  10. meta: {
  11. title: 'Permission',
  12. icon: 'lock',
  13. roles: ['admin', 'editor'] // you can set roles in root nav
  14. },
  15. children: [
  16. {
  17. path: 'page',
  18. component: () => import('@/views/permission/page'),
  19. name: 'PagePermission',
  20. meta: {
  21. title: 'Page Permission',
  22. roles: ['admin'] // or you can only set roles in sub nav
  23. }
  24. },
  25. {
  26. path: 'directive',
  27. component: () => import('@/views/permission/directive'),
  28. name: 'DirectivePermission',
  29. meta: {
  30. title: 'Directive Permission'
  31. // if do not set roles, means: this page does not require permission
  32. }
  33. },
  34. {
  35. path: 'role',
  36. component: () => import('@/views/permission/role'),
  37. name: 'RolePermission',
  38. meta: {
  39. title: 'Role Permission',
  40. roles: ['admin']
  41. }
  42. }
  43. ]
  44. },
  45. {
  46. path: '/icon',
  47. component: Layout,
  48. children: [
  49. {
  50. path: 'index',
  51. component: () => import('@/views/icons/index'),
  52. name: 'Icons',
  53. meta: { title: 'Icons', icon: 'icon', noCache: true }
  54. }
  55. ]
  56. },
  57. /** when your routing map is too long, you can split it into small modules **/
  58. componentsRouter,
  59. chartsRouter,