diff --git a/packages/shims-vue-runtime.d.ts b/packages/shims-vue-runtime.d.ts index eb4d695d2b1..b1fe7cdc681 100644 --- a/packages/shims-vue-runtime.d.ts +++ b/packages/shims-vue-runtime.d.ts @@ -75,6 +75,7 @@ declare module '@vue/runtime-core' { index: number data: Record }[] + $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 7a2e862a1fc..4365862c96c 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -5535,7 +5535,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 @@ -5741,7 +5753,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 f25d48f2d2a..0acf740a93a 100644 --- a/packages/uni-mp-vue/src/helpers/vOn.ts +++ b/packages/uni-mp-vue/src/helpers/vOn.ts @@ -24,6 +24,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 @@ -37,7 +38,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({