index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { getCurrentRoute, showLog } from './utils.js'
  2. // 用于存放页面相关信息
  3. const map = new Map()
  4. /* 保存页面进入时的时间及其它参数信息 */
  5. function addTime() {
  6. const route = getCurrentRoute()
  7. if (!route || map.has(route.path)) return
  8. const currentTime = Date.now()
  9. const data = { time: currentTime, ...route }
  10. map.set(route.path, data)
  11. return data
  12. }
  13. /* 获取页面离开时的时间及其它参数信息 */
  14. function getTime() {
  15. const route = getCurrentRoute()
  16. if (!map.has(route.path)) return
  17. const data = map.get(route.path)
  18. map.delete(route.path)
  19. return data
  20. }
  21. export default function(Vue, options = {}) {
  22. Vue.mixin({
  23. onLoad() {
  24. const res = addTime()
  25. if (!res) return
  26. if (typeof options.onEnter === 'function') {
  27. options.onEnter(res, map, showLog)
  28. }
  29. },
  30. onShow() {
  31. const res = addTime()
  32. if (!res) return
  33. if (typeof options.onEnter === 'function') {
  34. options.onEnter(res, map, showLog)
  35. }
  36. },
  37. onHide() {
  38. const res = getTime()
  39. if (!res) return
  40. if (typeof options.onLeave === 'function') {
  41. options.onLeave(res, map, showLog)
  42. }
  43. },
  44. beforeDestroy() {
  45. const res = getTime()
  46. if (!res) return
  47. if (typeof options.onLeave === 'function') {
  48. options.onLeave(res, map, showLog)
  49. }
  50. }
  51. })
  52. }