index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-10-14 17:17:53
  4. * @LastEditTime: 2022-04-25 16:08:49
  5. * @LastEditors: your name
  6. * @Description: In User Settings Edit
  7. * @FilePath: \Foshan4A\src\router\index.js
  8. */
  9. import Vue from 'vue'
  10. import Router from 'vue-router'
  11. import store from '@/store'
  12. Vue.use(Router)
  13. /* Layout */
  14. // import Layout from '@/layout'
  15. import routesOne from './routes/routes-file-one'
  16. import routesTwo from './routes/routes-file-two'
  17. import routesThree from './routes/routes-file-three'
  18. import routesFileFour from './routes/routes-file-four'
  19. /**
  20. * Note: sub-menu only appear when route children.length >= 1
  21. * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
  22. *
  23. * hidden: true if set true, item will not show in the sidebar(default is false)
  24. * alwaysShow: true if set true, will always show the root menu
  25. * if not set alwaysShow, when item has more than one children route,
  26. * it will becomes nested mode, otherwise not show the root menu
  27. * redirect: noRedirect if set noRedirect will no redirect in the breadcrumb
  28. * name:'router-name' the name is used by <keep-alive> (must set!!!)
  29. * meta : {
  30. roles: ['admin','editor'] control the page roles (you can set multiple roles)
  31. title: 'title' the name show in sidebar and breadcrumb (recommend set)
  32. icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
  33. breadcrumb: false if set false, the item will hidden in breadcrumb(default is true)
  34. activeMenu: '/example/list' if set path, the sidebar will highlight the path you set
  35. }
  36. */
  37. const routesPush = (arr, routes) => {
  38. if (!Array.isArray(arr)) return '请传入数组格式路由表'
  39. for (let i = 0; i < arr.length; i++) {
  40. routes.unshift(arr[i])
  41. }
  42. }
  43. /**
  44. * constantRoutes
  45. * a base page that does not have permission requirements
  46. * all roles can be accessed
  47. */
  48. export const constantRoutes = [
  49. {
  50. path: '/login',
  51. component: () => import('@/views/login/index'),
  52. hidden: true
  53. },
  54. {
  55. path: '/404',
  56. component: () => import('@/views/404'),
  57. hidden: true
  58. }
  59. // { path: '/', redirect: '/nopower', component: () => import('@/views/noPower'), hidden: true }
  60. // 404 page must be placed at the end !!!
  61. ]
  62. // 动态路由
  63. export const asyncRoutes = []
  64. // 插入路由
  65. routesPush(
  66. [
  67. ...routesOne,
  68. ...routesTwo,
  69. ...routesThree,
  70. ...routesFileFour
  71. ],
  72. asyncRoutes
  73. )
  74. // asyncRoutes.push({ path: '/', component: () => import('@/views/noPower'), hidden: true })
  75. asyncRoutes.push({ path: '*', component: () => import('@/views/404'), hidden: true })
  76. const createRouter = () =>
  77. new Router({
  78. // mode: 'history', // require service support
  79. scrollBehavior: () => ({ y: 0 }),
  80. routes: constantRoutes
  81. })
  82. const router = createRouter()
  83. router.beforeEach((to, from, next) => {
  84. // 如果 要 from(离开) 的页面是 keepAlive缓存的,
  85. // 再根据 deepth 来判断是前进还是后退
  86. if (from.meta.keepAlive) {
  87. if (to.meta.deepth && to.meta.deepth > from.meta.deepth) {
  88. store.dispatch('keepAlive/editPage', from)
  89. } else {
  90. store.dispatch('keepAlive/deletePage', to)
  91. }
  92. }
  93. // 如果要to(进入)的页面是需要keepAlive缓存的,把name push进keepAlive数组中
  94. if (to.meta.keepAlive) {
  95. const currentPage = store.state.keepAlive.keepAlivePages.find(page => page.name === to.name)
  96. if (currentPage) {
  97. if (currentPage.fullPath !== to.fullPath) {
  98. next(currentPage.fullPath)
  99. }
  100. } else {
  101. store.dispatch('keepAlive/addPage', to)
  102. }
  103. }
  104. next()
  105. })
  106. // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  107. export function resetRouter() {
  108. const newRouter = createRouter()
  109. router.matcher = newRouter.matcher // reset router
  110. }
  111. export default router