提交 3cbbd7a5 编写于 作者: fxy060608's avatar fxy060608

fix(mp): support mixin runtime hooks (question/140351)

上级 fa5e9586
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())
}
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册