From 9c0091f8b5fe63aa17f45c14c6f4f90c8d83c8c2 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 17 Jun 2019 18:06:31 +0800 Subject: [PATCH] fix(h5): add lifecycle strategies --- src/core/service/plugins/index.js | 8 +++- src/core/service/plugins/lifecycle.js | 57 +++++++++++++++++++++++++++ src/core/service/plugins/util.js | 20 ++++------ 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 src/core/service/plugins/lifecycle.js diff --git a/src/core/service/plugins/index.js b/src/core/service/plugins/index.js index f6c2ceea8..9c13783e3 100644 --- a/src/core/service/plugins/index.js +++ b/src/core/service/plugins/index.js @@ -16,6 +16,10 @@ import { createPageMixin } from './page' +import { + lifecycleMixin +} from './lifecycle' + import { getTabBarScrollPosition } from './app/router-guard' @@ -55,7 +59,9 @@ function getLocation (base = '/') { export default { install (Vue, { routes - } = {}) { + } = {}) { + lifecycleMixin(Vue) + const minId = getMinId(routes) const router = new VueRouter({ id: minId, diff --git a/src/core/service/plugins/lifecycle.js b/src/core/service/plugins/lifecycle.js new file mode 100644 index 000000000..620f7cae4 --- /dev/null +++ b/src/core/service/plugins/lifecycle.js @@ -0,0 +1,57 @@ +/* @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 + }) +} diff --git a/src/core/service/plugins/util.js b/src/core/service/plugins/util.js index 55819da95..b9f484167 100644 --- a/src/core/service/plugins/util.js +++ b/src/core/service/plugins/util.js @@ -1,9 +1,5 @@ -import { - isFn -} from 'uni-shared' - 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) { @@ -20,12 +16,12 @@ export function callPageHook (vm, hook, ...params) { vm.$mp.query = params[0] UniServiceJSBridge.publishHandler('onPageLoad', vm, vm.$page.id) } - if (hook === 'onShow') { - if ( - vm.$route.meta.isTabBar && - vm.$route.params.detail - ) { - UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail) + if (hook === 'onShow') { + if ( + vm.$route.meta.isTabBar && + vm.$route.params.detail + ) { + UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail) } UniServiceJSBridge.publishHandler('onPageShow', vm, vm.$page.id) } @@ -34,4 +30,4 @@ export function callPageHook (vm, hook, ...params) { console.debug(`${vm.$page.route}[${vm.$page.id}]:${hook} have been invoked`) } return callHook(vm, hook, params) -} +} -- GitLab