提交 722fec98 编写于 作者: fxy060608's avatar fxy060608

feat(mp): support custom lifecycle

上级 5342708b
import {
isUniLifecycleHook,
MINI_PROGRAM_PAGE_RUNTIME_HOOKS,
once,
ON_ADD_TO_FAVORITES,
......@@ -12,7 +13,7 @@ import {
ON_TAB_ITEM_TAP,
ON_UNLOAD,
} from '@dcloudio/uni-shared'
import { hasOwn, isArray, isFunction } from '@vue/shared'
import { hasOwn, isArray } from '@vue/shared'
import { ComponentOptions } from 'vue'
......@@ -42,7 +43,7 @@ function findHooks(
): Set<string> {
if (vueOptions) {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
if (isUniLifecycleHook(name, vueOptions[name])) {
hooks.add(name)
}
})
......
import { isFunction } from '@vue/shared'
import {
ON_ADD_TO_FAVORITES,
ON_BACK_PRESS,
......@@ -95,3 +96,23 @@ export const MINI_PROGRAM_PAGE_RUNTIME_HOOKS = /*#__PURE__*/ (() => {
onShareTimeline: 1 << 2,
} as const
})()
export function isUniLifecycleHook(
name: string,
value: unknown,
checkType = true
) {
// 检查类型
if (checkType && !isFunction(value)) {
return false
}
if (UniLifecycleHooks.indexOf(name as any) > -1) {
// 已预定义
return true
} else if (name.indexOf('on') === 0) {
// 以 on 开头
return true
}
return false
}
import { invokeHook } from '@dcloudio/uni-core'
import { LINEFEED, ON_LOAD, ON_SHOW } from '@dcloudio/uni-shared'
import {
isUniLifecycleHook,
LINEFEED,
ON_LOAD,
ON_SHOW,
} from '@dcloudio/uni-shared'
import { isArray, isFunction } from '@vue/shared'
import {
......@@ -31,8 +36,9 @@ export function initHooks(
// 仅 App,Page 类型支持在 options 中配置 on 生命周期,组件可以使用组合式 API 定义页面生命周期
return
}
Object.keys(options).forEach((name) => {
if (name.indexOf('on') === 0) {
if (isUniLifecycleHook(name, options[name], false)) {
const hooks = options[name]
if (isArray(hooks)) {
hooks.forEach((hook) =>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册