diff --git a/packages/shims-vue-runtime.d.ts b/packages/shims-vue-runtime.d.ts index 1af2017c1be..81db900cb40 100644 --- a/packages/shims-vue-runtime.d.ts +++ b/packages/shims-vue-runtime.d.ts @@ -50,6 +50,7 @@ declare module '@vue/runtime-core' { // mp $updateScopedSlots: () => void $scopedSlotsData?: { path: string; index: number; data: Data }[] + $currentSlotComponentInstance?: ComponentInternalInstance // h5 | app $wxsModules?: string[] // 暂定 h5 diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index c456e2e48af..8e96b152d87 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -7781,7 +7781,19 @@ function vOn(value, key) { (isString(key) || typeof key === 'number') ? '_' + key : ''; - const name = 'e' + instance.$ei++ + extraKey; + let name; + if (instance.$currentSlotComponentInstance) { + const slotComponentInstance = instance.$currentSlotComponentInstance; + name = + 'e' + + slotComponentInstance.uid + + '_' + + slotComponentInstance.$ei++ + + extraKey; + } + else { + name = 'e' + instance.$ei++ + extraKey; + } const mpInstance = ctx.$scope; if (!value) { // remove @@ -7982,7 +7994,9 @@ function createScopedSlotInvoker(instance) { index = index || 0; // 确保当前 slot 的上下文,类似 withCtx const prevInstance = setCurrentRenderingInstance(instance); + instance.$currentSlotComponentInstance = prevInstance; const data = slot.fn(args, slotName + (hasIndex ? '-' + index : ''), index); + delete instance.$currentSlotComponentInstance; const path = slot.fn.path; setCurrentRenderingInstance(prevInstance); (instance.$scopedSlotsData || (instance.$scopedSlotsData = [])).push({ diff --git a/packages/uni-mp-vue/src/helpers/vOn.ts b/packages/uni-mp-vue/src/helpers/vOn.ts index 331aecebdf5..c2b91c71322 100644 --- a/packages/uni-mp-vue/src/helpers/vOn.ts +++ b/packages/uni-mp-vue/src/helpers/vOn.ts @@ -23,6 +23,7 @@ interface Invoker { export function vOn(value: EventValue | undefined, key?: number | string) { const instance = getCurrentInstance()! as unknown as { $ei: number + $currentSlotComponentInstance?: { uid: number; $ei: number } ctx: { $scope: Record; $mpPlatform: UniApp.PLATFORM } } const ctx = instance.ctx @@ -36,7 +37,19 @@ export function vOn(value: EventValue | undefined, key?: number | string) { ? '_' + key : '' - const name = 'e' + instance.$ei++ + extraKey + let name: string + + if (instance.$currentSlotComponentInstance) { + const slotComponentInstance = instance.$currentSlotComponentInstance + name = + 'e' + + slotComponentInstance.uid + + '_' + + slotComponentInstance.$ei++ + + extraKey + } else { + name = 'e' + instance.$ei++ + extraKey + } const mpInstance = ctx.$scope if (!value) { diff --git a/packages/uni-mp-vue/src/helpers/withScopedSlot.ts b/packages/uni-mp-vue/src/helpers/withScopedSlot.ts index b3d877e9784..9dc53812c0f 100644 --- a/packages/uni-mp-vue/src/helpers/withScopedSlot.ts +++ b/packages/uni-mp-vue/src/helpers/withScopedSlot.ts @@ -65,7 +65,9 @@ function createScopedSlotInvoker(instance: ComponentInternalInstance) { index = index || 0 // 确保当前 slot 的上下文,类似 withCtx const prevInstance = setCurrentRenderingInstance(instance) + instance.$currentSlotComponentInstance = prevInstance const data = slot.fn(args, slotName + (hasIndex ? '-' + index : ''), index) + delete instance.$currentSlotComponentInstance const path = slot.fn.path setCurrentRenderingInstance(prevInstance) ;(instance.$scopedSlotsData || (instance.$scopedSlotsData = [])).push({