From f4cf576157b9dd9bebe3e54d9a0fca12bda7bcdb Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 20 Oct 2021 19:45:13 +0800 Subject: [PATCH] wip(mp): event --- packages/uni-mp-compiler/__tests__/vModel.spec.ts | 4 ++-- packages/uni-mp-compiler/src/transforms/vModel.ts | 2 +- packages/uni-mp-vue/dist/vue.runtime.esm.js | 6 +++++- packages/uni-mp-vue/src/helpers/vOn.ts | 12 +++++++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/uni-mp-compiler/__tests__/vModel.spec.ts b/packages/uni-mp-compiler/__tests__/vModel.spec.ts index bf6938e00..8f16af191 100644 --- a/packages/uni-mp-compiler/__tests__/vModel.spec.ts +++ b/packages/uni-mp-compiler/__tests__/vModel.spec.ts @@ -6,7 +6,7 @@ describe('compiler: transform v-model', () => { ``, ``, `(_ctx, _cache) => { - return { a: _ctx.model, b: _vOn($event => _ctx.model = $event.detail.__args__[0]) } + return { a: _ctx.model, b: _vOn($event => _ctx.model = $event) } }` ) }) @@ -15,7 +15,7 @@ describe('compiler: transform v-model', () => { ``, ``, `(_ctx, _cache) => { - return { a: _ctx.model, b: _vOn($event => _ctx.model = $event.detail.__args__[0]) } + return { a: _ctx.model, b: _vOn($event => _ctx.model = $event) } }`, { cacheHandlers: true, diff --git a/packages/uni-mp-compiler/src/transforms/vModel.ts b/packages/uni-mp-compiler/src/transforms/vModel.ts index 311cbf673..f36aa3e6f 100644 --- a/packages/uni-mp-compiler/src/transforms/vModel.ts +++ b/packages/uni-mp-compiler/src/transforms/vModel.ts @@ -125,7 +125,7 @@ function transformComponentVModel( ): DirectiveNode[] { return transformVModel(props, context, { formatEventCode(code) { - return code.replace(`= $event`, `= $event.detail.__args__[0]`) + return code }, }) } diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index f5779c759..2d71eb1c2 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -5014,7 +5014,11 @@ function vOn(value) { } function createInvoker(initialValue, instance) { const invoker = (e) => { - callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, [e]); + let args = [e]; + if (e.detail && e.detail.__args__) { + args = e.detail.__args__; + } + callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, args); }; invoker.value = initialValue; return invoker; diff --git a/packages/uni-mp-vue/src/helpers/vOn.ts b/packages/uni-mp-vue/src/helpers/vOn.ts index 51401efe0..87f24c516 100644 --- a/packages/uni-mp-vue/src/helpers/vOn.ts +++ b/packages/uni-mp-vue/src/helpers/vOn.ts @@ -38,16 +38,26 @@ export function vOn(value: EventValue | undefined) { return name } +interface MPEvent extends Event { + detail: { + __args__?: unknown[] + } +} + function createInvoker( initialValue: EventValue, instance: ComponentInternalInstance | null ) { const invoker: Invoker = (e: Event) => { + let args: unknown[] = [e] + if ((e as MPEvent).detail && (e as MPEvent).detail.__args__) { + args = (e as MPEvent).detail.__args__! + } callWithAsyncErrorHandling( patchStopImmediatePropagation(e, invoker.value), instance, ErrorCodes.NATIVE_EVENT_HANDLER, - [e] + args ) } invoker.value = initialValue -- GitLab