1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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() {
- const res = getTime()
- if (!res) return
- if (typeof options.onLeave === 'function') {
- options.onLeave(res, map, showLog)
- }
- },
- beforeDestroy() {
- const res = getTime()
- if (!res) return
- if (typeof options.onLeave === 'function') {
- options.onLeave(res, map, showLog)
- }
- }
- })
- }
|