12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import { getCurrentRoute } from './utils.js'
- /*默认配置*/
- const defaultOptions = {
- strict: false,
- enter(current) {},
- leave(prev) {},
- routting(current, prev) {}
- }
- // 用于存放页面路由信息
- const map = new Map()
- // 用于存放上一个页面路由信息
- let prev = null
- function behavior(options) {
- const mixin = {}
- options = { ...defaultOptions, ...options }
- // 页面打开
- mixin.onLoad = function() {
- const route = getCurrentRoute()
- if (!route) return
- if (map.has(route.path)) return
- map.set(route.path, route)
- options.enter(route)
- options.routting(route, prev)
- }
- // 页面显示
- mixin.onShow = function() {
- if (options.strict) return
- mixin.onLoad()
- }
- // 页面隐藏
- mixin.onHide = function() {
- if (options.strict) return
- mixin.onUnload()
- }
- // 页面关闭
- mixin.onUnload = function() {
- prev = null
- const route = getCurrentRoute()
- if (!route) return
- if (!map.has(route.path)) return
- prev = map.get(route.path)
- map.delete(prev.path)
- options.leave(prev)
- }
- return mixin
- }
- export default behavior
|