提交 9c0091f8 编写于 作者: fxy060608's avatar fxy060608

fix(h5): add lifecycle strategies

上级 bac62d59
...@@ -16,6 +16,10 @@ import { ...@@ -16,6 +16,10 @@ import {
createPageMixin createPageMixin
} from './page' } from './page'
import {
lifecycleMixin
} from './lifecycle'
import { import {
getTabBarScrollPosition getTabBarScrollPosition
} from './app/router-guard' } from './app/router-guard'
...@@ -55,7 +59,9 @@ function getLocation (base = '/') { ...@@ -55,7 +59,9 @@ function getLocation (base = '/') {
export default { export default {
install (Vue, { install (Vue, {
routes routes
} = {}) { } = {}) {
lifecycleMixin(Vue)
const minId = getMinId(routes) const minId = getMinId(routes)
const router = new VueRouter({ const router = new VueRouter({
id: minId, id: minId,
......
/* @flow */
const LIFECYCLE_HOOKS = [
// App
'onLaunch',
'onShow',
'onHide',
'onUniNViewMessage',
'onError',
// Page
'onLoad',
// 'onShow',
'onReady',
// 'onHide',
'onUnload',
'onPullDownRefresh',
'onReachBottom',
'onTabItemTap',
'onShareAppMessage',
'onResize',
'onPageScroll',
'onNavigationBarButtonTap',
'onBackPress',
'onNavigationBarSearchInputChanged',
'onNavigationBarSearchInputConfirmed',
'onNavigationBarSearchInputClicked',
// Component
// 'onReady', // 兼容旧版本,应该移除该事件
'onPageShow',
'onPageHide',
'onPageResize'
]
export function lifecycleMixin (Vue) {
// fixed vue-class-component
const oldExtend = Vue.extend
Vue.extend = function (extendOptions) {
extendOptions = extendOptions || {}
const methods = extendOptions.methods
if (methods) {
Object.keys(methods).forEach(methodName => {
if (LIFECYCLE_HOOKS.indexOf(methodName) !== -1) {
extendOptions[methodName] = methods[methodName]
delete methods[methodName]
}
})
}
return oldExtend.call(this, extendOptions)
}
const strategies = Vue.config.optionMergeStrategies
const mergeHook = strategies.created
LIFECYCLE_HOOKS.forEach(hook => {
strategies[hook] = mergeHook
})
}
import {
isFn
} from 'uni-shared'
function callHook (vm, hook, params) { function callHook (vm, hook, params) {
return isFn(vm.$options[hook]) && vm.$options[hook].apply(vm, params) return vm.__call_hook(hook, params)
} }
export function callAppHook (vm, hook, ...params) { export function callAppHook (vm, hook, ...params) {
...@@ -20,12 +16,12 @@ export function callPageHook (vm, hook, ...params) { ...@@ -20,12 +16,12 @@ export function callPageHook (vm, hook, ...params) {
vm.$mp.query = params[0] vm.$mp.query = params[0]
UniServiceJSBridge.publishHandler('onPageLoad', vm, vm.$page.id) UniServiceJSBridge.publishHandler('onPageLoad', vm, vm.$page.id)
} }
if (hook === 'onShow') { if (hook === 'onShow') {
if ( if (
vm.$route.meta.isTabBar && vm.$route.meta.isTabBar &&
vm.$route.params.detail vm.$route.params.detail
) { ) {
UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail) UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail)
} }
UniServiceJSBridge.publishHandler('onPageShow', vm, vm.$page.id) UniServiceJSBridge.publishHandler('onPageShow', vm, vm.$page.id)
} }
...@@ -34,4 +30,4 @@ export function callPageHook (vm, hook, ...params) { ...@@ -34,4 +30,4 @@ export function callPageHook (vm, hook, ...params) {
console.debug(`${vm.$page.route}[${vm.$page.id}]:${hook} have been invoked`) console.debug(`${vm.$page.route}[${vm.$page.id}]:${hook} have been invoked`)
} }
return callHook(vm, hook, params) return callHook(vm, hook, params)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册