From c876755336dadb38ca0e687b8e63bde12d4189d5 Mon Sep 17 00:00:00 2001 From: acyza <101238421+acyza@users.noreply.github.com> Date: Sat, 13 Jul 2024 07:28:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=92=E6=A7=BD=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=B1=80=E9=83=A8=E6=9B=B4=E6=96=B0=E5=AF=BC=E8=87=B4=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6id=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/shims-vue-runtime.d.ts | 1 + packages/uni-mp-vue/dist/vue.runtime.esm.js | 16 +++++++++++++++- packages/uni-mp-vue/src/helpers/vOn.ts | 15 ++++++++++++++- .../uni-mp-vue/src/helpers/withScopedSlot.ts | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/shims-vue-runtime.d.ts b/packages/shims-vue-runtime.d.ts index f0ee0a5dff6..44c55185572 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({