router.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { createRouter, createWebHistory } from "vue-router";
  2. import useRouterStore from "@store/useRouterStore.js";
  3. import { routerEventEmitter } from "@store/useRouterStore.js";
  4. const routes = [
  5. {
  6. path: '/',
  7. redirect: '/login',
  8. component: () => import("@pages/login.vue")
  9. }, {
  10. path: '/login',
  11. name: 'login',
  12. meta: { title: '登录' },
  13. component: () => import("@pages/login.vue"),
  14. }, {
  15. path: '/home',
  16. name: 'home',
  17. meta: { title: '首页' },
  18. component: () => import("@pages/tabbar/home/index.vue"),
  19. }, {
  20. path: '/my',
  21. name: 'my',
  22. meta: { title: '我的' },
  23. component: () => import("@pages/tabbar/my/index.vue"),
  24. }, {
  25. path: '/news',
  26. name: 'news',
  27. meta: { title: '消息' },
  28. component: () => import("@pages/tabbar/news/index.vue"),
  29. }, {
  30. path: '/work',
  31. name: 'work',
  32. meta: { title: '工作' },
  33. component: () => import("@pages/tabbar/work/index.vue"),
  34. }, {
  35. path: '/moduleList',
  36. name: 'moduleList',
  37. meta: { title: '模块列表' },
  38. component: () => import("@pages/moduleList/moduleList.vue"),
  39. }, {
  40. path: '/details',
  41. name: 'details',
  42. meta: { title: '模块详情' },
  43. component: () => import("@pages/moduleDetails/index.vue"),
  44. }, {
  45. path: '/addEditor',
  46. name: 'addEditor',
  47. meta: { title: '模块新增编辑' },
  48. component: () => import("@pages/addEditor/index.vue"),
  49. },{
  50. path: '/visitor',
  51. name: 'visitor',
  52. meta: { title: '访客计划' },
  53. component: () => import("@pages/visitor/index.vue"),
  54. }, {
  55. path: '/addEditorVisitor',
  56. name: 'addEditorVisitor',
  57. meta: { title: '新增编辑访客计划' },
  58. component: () => import("@pages/visitorProgram/addEditorVisitor.vue"),
  59. }, {
  60. path: '/visitorDetails',
  61. name: 'visitorDetails',
  62. meta: { title: '访客计划详情' },
  63. component: () => import("@pages/visitorProgram/visitorDetails.vue"),
  64. }, {
  65. path: '/pdfPreview',
  66. name: 'pdfPreview',
  67. meta: { title: 'PDF预览' },
  68. component: () => import("@pages/pdfPreview/index.vue"),
  69. },{
  70. path: '/guideAttentionToOfficialAccount',
  71. name: 'guideAttentionToOfficialAccount',
  72. meta: { title: '引导关注公众号' },
  73. component: () => import("@pages/guide.vue"),
  74. }, {
  75. path: '/:pathMatch(.*)*',
  76. name: 'notFound',
  77. meta: { title: '404' },
  78. component: () => import("@pages/404.vue"),
  79. }
  80. ]
  81. const router = createRouter({
  82. history: createWebHistory(),
  83. routes
  84. });
  85. window.addEventListener('load', () => {
  86. const routerStore = useRouterStore();
  87. const currentPage = routerStore.currentPages[routerStore.currentPages.length - 1];
  88. routerStore.resetCacheStatus();//刷新页面重置页面缓存状态
  89. if (currentPage?.params?.length) {
  90. const currentPageParams = currentPage.params[0];
  91. const eventName = Reflect.ownKeys(currentPageParams)[0];
  92. setTimeout(() => {
  93. if (routerEventEmitter.getListeners(`${location.pathname}.${eventName}`).length) {
  94. routerEventEmitter.emit(`${location.pathname}.${eventName}`, currentPageParams[eventName]);
  95. }
  96. }, 100)
  97. }
  98. });
  99. /*监听浏览器历史记录发生变化*/
  100. window.addEventListener('popstate', (event) => {
  101. const routerStore = useRouterStore();
  102. const toPathName = event.state.current.slice(1);
  103. const backPathName = routerStore.currentPages[routerStore.currentPages.length - 2]?.pathName; //返回页面pathName
  104. const forwardPathName = routerStore.historyPages[0]?.pathName; //前进页pathName
  105. let currentPage = {}; //前进或者后退成功的页面
  106. if (toPathName == backPathName) { //浏览器返回上一级页面
  107. currentPage = { ...routerStore.currentPages[routerStore.currentPages.length - 2] }; //返回页路由
  108. routerStore.delete(1);
  109. } else if (toPathName == forwardPathName) {//浏览器前进
  110. const forwardPage = { ...routerStore.historyPages[0] }; //前进页路由
  111. routerStore.deleteHistory(toPathName, true);
  112. currentPage = forwardPage;
  113. }
  114. //页面再次刷新之后,页面缓存会清空,处理页面缓存清空之后传参失效问题
  115. if (!currentPage.cache) {
  116. const currentPageParams = currentPage.params[0] || {};
  117. const eventName = Reflect.ownKeys(currentPageParams)[0];
  118. setTimeout(() => {
  119. if (routerEventEmitter.getListeners(`${location.pathname}.${eventName}`).length) {
  120. routerEventEmitter.emit(`${location.pathname}.${eventName}`, currentPageParams[eventName]);
  121. }
  122. }, 100)
  123. }
  124. });
  125. export default router