From 3cbbd7a545a44d379d5b9c9e03b1ed556bd9aec4 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 1 Mar 2022 15:36:40 +0800 Subject: [PATCH] fix(mp): support mixin runtime hooks (question/140351) --- .../uni-mp-core/src/runtime/componentHooks.ts | 28 ++++++++++++++++++- packages/uni-mp-core/src/runtime/page.ts | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/uni-mp-core/src/runtime/componentHooks.ts b/packages/uni-mp-core/src/runtime/componentHooks.ts index 08144a733..cb21271dd 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 5aa10e968..51d63f166 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 -- GitLab