index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆onHide')
  39. const res = getTime()
  40. if (!res) return
  41. if (typeof options.onLeave === 'function') {
  42. options.onLeave(res, map, showLog)
  43. }
  44. },
  45. onUnload() {
  46. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆onUnload')
  47. const res = getTime()
  48. if (!res) return
  49. if (typeof options.onLeave === 'function') {
  50. options.onLeave(res, map, showLog)
  51. }
  52. },
  53. beforeDestroy() {
  54. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆beforeDestroy')
  55. const res = getTime()
  56. if (!res) return
  57. if (typeof options.onLeave === 'function') {
  58. options.onLeave(res, map, showLog)
  59. }
  60. }
  61. })
  62. }