diff --git a/packages/uni-mp-core/src/runtime/componentHooks.ts b/packages/uni-mp-core/src/runtime/componentHooks.ts index 08144a733134a4737123621a3934412a4269201b..cb21271dd3b2dcd30110c1ed0e91f8c36419b2f5 100644 --- a/packages/uni-mp-core/src/runtime/componentHooks.ts +++ b/packages/uni-mp-core/src/runtime/componentHooks.ts @@ -1,5 +1,6 @@ import { MINI_PROGRAM_PAGE_RUNTIME_HOOKS, + once, ON_ADD_TO_FAVORITES, ON_HIDE, ON_LOAD, @@ -11,7 +12,7 @@ import { ON_TAB_ITEM_TAP, ON_UNLOAD, } from '@dcloudio/uni-shared' -import { hasOwn, isFunction } from '@vue/shared' +import { hasOwn, isArray, isFunction } from '@vue/shared' import { ComponentOptions } from 'vue' @@ -113,3 +114,28 @@ export function initRuntimeHooks( } }) } + +const findMixinRuntimeHooks = /*#__PURE__*/ once(() => { + const runtimeHooks: string[] = [] + const app = getApp({ allowDefault: true }) + if (app && app.$vm && app.$vm.$) { + const mixins = app.$vm.$.appContext.mixins as ComponentOptions[] + if (isArray(mixins)) { + const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS) + mixins.forEach((mixin) => { + hooks.forEach((hook) => { + if (hasOwn(mixin, hook) && !runtimeHooks.includes(hook)) { + runtimeHooks.push(hook) + } + }) + }) + } + } + return runtimeHooks +}) + +export function initMixinRuntimeHooks( + mpOptions: MiniProgramAppOptions | WechatMiniprogram.Component.MethodOption +) { + initHooks(mpOptions, findMixinRuntimeHooks()) +} diff --git a/packages/uni-mp-core/src/runtime/page.ts b/packages/uni-mp-core/src/runtime/page.ts index 5aa10e96801eb01f1f2ec833eae6fb03ee0cc61f..51d63f1668db0872af670187d7c6fcadf2cb9242 100644 --- a/packages/uni-mp-core/src/runtime/page.ts +++ b/packages/uni-mp-core/src/runtime/page.ts @@ -12,6 +12,7 @@ import { initHooks, initUnknownHooks, initRuntimeHooks, + initMixinRuntimeHooks, } from './componentHooks' import { initPageProps } from './componentProps' @@ -51,6 +52,7 @@ function parsePage( initHooks(methods, PAGE_INIT_HOOKS) initUnknownHooks(methods, vueOptions) initRuntimeHooks(methods, vueOptions.__runtimeHooks) + initMixinRuntimeHooks(methods) parse && parse(miniProgramPageOptions, { handleLink }) return miniProgramPageOptions