index.js 3.6 KB

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