diff --git a/packages/taro-runtime/src/dom/event.ts b/packages/taro-runtime/src/dom/event.ts index 08601d11e6a1..c708d3c764b9 100644 --- a/packages/taro-runtime/src/dom/event.ts +++ b/packages/taro-runtime/src/dom/event.ts @@ -3,6 +3,7 @@ import { EMPTY_OBJ } from '@tarojs/shared' import { document } from '../bom/document' import { TaroElement } from './element' import { CurrentReconciler } from '../reconciler' +import { isParentBinded } from '../utils' interface EventOptions { bubbles: boolean; @@ -107,9 +108,8 @@ export function eventHandler (event: MpEvent) { } if (typeof CurrentReconciler.batchedEventUpdates === 'function') { const type = event.type - const isParentBinded = node.parentElement?.__handlers[type]?.length - if (!isParentBinded || (type === 'touchmove' && !!node.props.catchMove)) { + if (!isParentBinded(node, type) || (type === 'touchmove' && !!node.props.catchMove)) { // 最上层组件统一 batchUpdate CurrentReconciler.batchedEventUpdates(() => { if (eventsBatch[type]) { diff --git a/packages/taro-runtime/src/utils/index.ts b/packages/taro-runtime/src/utils/index.ts index 2f79fc6e226f..73c5a1dc5dbb 100644 --- a/packages/taro-runtime/src/utils/index.ts +++ b/packages/taro-runtime/src/utils/index.ts @@ -22,3 +22,20 @@ export function isHasExtractProp (el: TaroElement): boolean { }) return Boolean(res) } + +/** + * 往上寻找组件树直到 root,寻找是否有祖先组件绑定了同类型的事件 + * @param node 当前组件 + * @param type 事件类型 + */ +export function isParentBinded (node: TaroElement | null, type: string): boolean { + let res = false + while (node?.parentElement && node.parentElement._path !== 'root') { + if (node.parentElement.__handlers[type]?.length) { + res = true + break + } + node = node.parentElement + } + return res +}