index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-10-14 17:17:53
  4. * @LastEditTime: 2022-06-10 10:17:05
  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. import routesFive from './routes/routes-file-five'
  20. import routesFileSix from './routes/routes-file-six'
  21. /**
  22. * Note: sub-menu only appear when route children.length >= 1
  23. * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
  24. *
  25. * hidden: true if set true, item will not show in the sidebar(default is false)
  26. * alwaysShow: true if set true, will always show the root menu
  27. * if not set alwaysShow, when item has more than one children route,
  28. * it will becomes nested mode, otherwise not show the root menu
  29. * redirect: noRedirect if set noRedirect will no redirect in the breadcrumb
  30. * name:'router-name' the name is used by <keep-alive> (must set!!!)
  31. * meta : {
  32. roles: ['admin','editor'] control the page roles (you can set multiple roles)
  33. title: 'title' the name show in sidebar and breadcrumb (recommend set)
  34. icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
  35. breadcrumb: false if set false, the item will hidden in breadcrumb(default is true)
  36. activeMenu: '/example/list' if set path, the sidebar will highlight the path you set
  37. }
  38. */
  39. const routesPush = (arr, routes) => {
  40. if (!Array.isArray(arr)) return '请传入数组格式路由表'
  41. for (let i = 0; i < arr.length; i++) {
  42. routes.unshift(arr[i])
  43. }
  44. }
  45. /**
  46. * constantRoutes
  47. * a base page that does not have permission requirements
  48. * all roles can be accessed
  49. */
  50. export const constantRoutes = [
  51. {
  52. path: '/login',
  53. component: () => import('@/views/login/index'),
  54. hidden: true
  55. },
  56. {
  57. path: '/404',
  58. component: () => import('@/views/404'),
  59. hidden: true
  60. }
  61. // { path: '/', redirect: '/nopower', component: () => import('@/views/noPower'), hidden: true }
  62. // 404 page must be placed at the end !!!
  63. ]
  64. // 动态路由
  65. export const asyncRoutes = []
  66. // 插入路由
  67. routesPush([...routesOne, ...routesTwo, ...routesFileFour, ...routesFive, ...routesFileSix], asyncRoutes)
  68. // asyncRoutes.push({ path: '/', component: () => import('@/views/noPower'), hidden: true })
  69. asyncRoutes.push({
  70. path: '*',
  71. component: () => import('@/views/404'),
  72. hidden: true
  73. })
  74. const createRouter = () =>
  75. new Router({
  76. // mode: 'history', // require service support
  77. scrollBehavior: () => ({ y: 0 }),
  78. routes: constantRoutes
  79. })
  80. const router = createRouter()
  81. router.beforeEach((to, from, next) => {
  82. if (
  83. (from.path.includes('flightView') && from.query.flightNO && from.query.flightDate) ||
  84. (from.path.includes('containerView') &&
  85. from.query.flightNO &&
  86. from.query.flightDate &&
  87. from.query.departureAirport &&
  88. from.query.landingAirport &&
  89. from.query.containerID)
  90. ) {
  91. store.dispatch('keepAlive/savePage', from)
  92. }
  93. if (
  94. from.meta?.keepAlive &&
  95. ((to.path.includes('flightView') && (!to.query.flightNO || !to.query.flightDate)) ||
  96. (to.path.includes('containerView') &&
  97. (!to.query.flightNO ||
  98. !to.query.flightDate ||
  99. !to.query.departureAirport ||
  100. !to.query.landingAirport ||
  101. !to.query.containerID)))
  102. ) {
  103. const savedPage = store.getters.savedPages.find(savedPage => savedPage.name === to.name)
  104. if (savedPage) {
  105. next(savedPage.fullPath)
  106. }
  107. }
  108. next()
  109. })
  110. // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  111. export function resetRouter() {
  112. const newRouter = createRouter()
  113. router.matcher = newRouter.matcher // reset router
  114. }
  115. export default router