123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import { getCurrentRoute, showLog } from './utils.js'
- // 用于存放页面相关信息
- const map = new Map()
- /* 保存页面进入时的时间及其它参数信息 */
- function addTime() {
- const route = getCurrentRoute()
- if (!route || map.has(route.path)) return
- const currentTime = Date.now()
- const data = { time: currentTime, ...route }
- map.set(route.path, data)
- return data
- }
- /* 获取页面离开时的时间及其它参数信息 */
- function getTime() {
- const route = getCurrentRoute()
- if (!map.has(route.path)) return
- const data = map.get(route.path)
- map.delete(route.path)
- return data
- }
- export default function(Vue, options = {}) {
- Vue.mixin({
- onLoad() {
- const res = addTime()
- if (!res) return
- if (typeof options.onEnter === 'function') {
- options.onEnter(res, map, showLog)
- }
- },
- onShow() {
- const res = addTime()
- if (!res) return
- if (typeof options.onEnter === 'function') {
- options.onEnter(res, map, showLog)
- }
- },
- onHide() {
- console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆onHide')
- const res = getTime()
- if (!res) return
- if (typeof options.onLeave === 'function') {
- options.onLeave(res, map, showLog)
- }
- },
- onUnload() {
- console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆onUnload')
- const res = getTime()
- if (!res) return
- if (typeof options.onLeave === 'function') {
- options.onLeave(res, map, showLog)
- }
- },
- beforeDestroy() {
- console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆beforeDestroy')
- const res = getTime()
- if (!res) return
- if (typeof options.onLeave === 'function') {
- options.onLeave(res, map, showLog)
- }
- }
- })
- }
|