index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import store from '@/store'
  4. declare const _hmt: any
  5. if (process.env.NODE_DEV !== 'production') {
  6. Vue.use(VueRouter)
  7. }
  8. const originalReplace: any = VueRouter.prototype.replace
  9. VueRouter.prototype.replace = function replace (location) {
  10. return originalReplace.call(this, location).catch((error: any) => error)
  11. }
  12. let routes = [
  13. {
  14. path: '/',
  15. redirect: 'home'
  16. },
  17. {
  18. path: '/home',
  19. name: 'home',
  20. component: () => import(/* webpackChunkName: "home" */ '@/views/Home.vue'),
  21. meta: {
  22. title: '剑鱼文库',
  23. layoutConf: true,
  24. isBack: false
  25. }
  26. },
  27. {
  28. path: '/404',
  29. name: '404',
  30. component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue'),
  31. meta: {
  32. title: '404'
  33. }
  34. }
  35. ]
  36. const routerContext = require.context('./modules', true, /\.ts$/)
  37. routerContext.keys().forEach(route => {
  38. // 如果是根目录的 index.js 、不处理
  39. if (route.startsWith('./index')) {
  40. return
  41. }
  42. const routerModule = routerContext(route)
  43. /**
  44. * 兼容 import export 和 require module.export 两种规范
  45. */
  46. routes = routes.concat(routerModule.default || routerModule)
  47. })
  48. const tempObj: any = {
  49. path: '*',
  50. redirect: {
  51. name: '404'
  52. }
  53. }
  54. routes = routes.concat(tempObj)
  55. const myRouter = new VueRouter({
  56. mode: 'history', // require service support
  57. base: process.env.BASE_URL,
  58. scrollBehavior: () => ({ x: 0, y: 0 }),
  59. routes
  60. })
  61. const history = window.sessionStorage
  62. const tempHist: any = history.getItem('count')
  63. let historyCount: any = (tempHist * 1) || 0
  64. history.setItem('/', String(0))
  65. myRouter.beforeEach((to, from, next) => {
  66. // 设置页面标题
  67. document.title = (to as any).meta.title
  68. store.commit('updatePageTitle', (to as any).meta.title)
  69. try {
  70. ;(_hmt as any).push(['_trackPageview', to.fullPath])
  71. } catch (error) {
  72. console.log(error)
  73. }
  74. // 页面切换动画
  75. if (to.params.direction) {
  76. store.commit('updateDirection', to.params.direction)
  77. } else {
  78. const toIndex = history.getItem(to.path)
  79. const fromIndex = history.getItem(from.path)
  80. // 判断并记录跳转页面是否访问过,以此判断跳转过渡方式
  81. if (toIndex) {
  82. if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
  83. store.commit('updateDirection', 'forward')
  84. } else {
  85. store.commit('updateDirection', 'back')
  86. }
  87. } else {
  88. ++historyCount
  89. history.setItem('count', String(historyCount))
  90. to.path !== '/' && history.setItem(to.path, String(historyCount))
  91. store.commit('updateDirection', 'forward')
  92. }
  93. }
  94. // 对认证伙伴组件进行动态缓存
  95. if (to.name === 'auth-partner') {
  96. store.commit('isKeepAlive', to.name)
  97. }
  98. next()
  99. })
  100. // myRouter.afterEach((to, from) => {
  101. // // 全局路由守卫
  102. // })
  103. export default myRouter