From e5eb8c38a0244c6ae24addf8c327d60ab0ed76f7 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 15 Dec 2021 14:14:35 +0800 Subject: [PATCH] fix(mp): support defineExpose (#3074) --- packages/uni-mp-alipay/dist/uni.mp.esm.js | 11 +++++++++-- packages/uni-mp-alipay/src/runtime/util.ts | 3 ++- packages/uni-mp-baidu/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-mp-core/src/index.ts | 1 + packages/uni-mp-core/src/runtime/util.ts | 14 ++++++++++++-- packages/uni-mp-kuaishou/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-mp-lark/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-mp-qq/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-mp-toutiao/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-mp-vue/dist/vue.runtime.esm.js | 5 +++-- packages/uni-mp-vue/lib/vue.runtime.esm.js | 5 +++-- packages/uni-mp-weixin/dist/uni.mp.esm.js | 13 ++++++++++--- packages/uni-quickapp-webview/dist/uni.mp.esm.js | 13 ++++++++++--- 13 files changed, 100 insertions(+), 30 deletions(-) diff --git a/packages/uni-mp-alipay/dist/uni.mp.esm.js b/packages/uni-mp-alipay/dist/uni.mp.esm.js index a9b084eb7..a5bccdacc 100644 --- a/packages/uni-mp-alipay/dist/uni.mp.esm.js +++ b/packages/uni-mp-alipay/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, capitalize, isFunction, extend, isString, camelize } from '@vue/shared'; -import { injectHook, ref, toRaw, findComponentPropsData, updateProps, invalidateJob, EMPTY_OBJ, isRef, setTemplateRef, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, getExposeProxy, toRaw, findComponentPropsData, updateProps, invalidateJob, EMPTY_OBJ, isRef, setTemplateRef, pruneComponentPropsCache } from 'vue'; // quickapp-webview 不能使用 default 作为插槽名称 const SLOT_DEFAULT_NAME = 'd'; @@ -417,6 +417,13 @@ function initWxsCallMethods(methods, wxsCallMethods) { }; }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function findVmByVueId(instance, vuePid) { // 标准 vue3 中 没有 $children,定制了内核 const $children = instance.$children; @@ -728,7 +735,7 @@ function handleRef(ref) { const instance = this.$vm.$; const refs = instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs; const { setupState } = instance; - const refValue = ref.$vm || ref; + const refValue = findRefValue(ref); if (refName) { if (isString(refName)) { refs[refName] = refValue; diff --git a/packages/uni-mp-alipay/src/runtime/util.ts b/packages/uni-mp-alipay/src/runtime/util.ts index f43ca140f..5f9aae341 100644 --- a/packages/uni-mp-alipay/src/runtime/util.ts +++ b/packages/uni-mp-alipay/src/runtime/util.ts @@ -24,6 +24,7 @@ import { initComponentInstance, CreateComponentOptions, updateComponentProps, + findRefValue, } from '@dcloudio/uni-mp-core' import { handleLink as handleBaseLink } from '@dcloudio/uni-mp-weixin' @@ -150,7 +151,7 @@ export function handleRef(this: MPComponentInstance, ref: MPComponentInstance) { instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs const { setupState } = instance - const refValue = ref.$vm || ref + const refValue = findRefValue(ref as any) if (refName) { if (isString(refName)) { refs[refName] = refValue diff --git a/packages/uni-mp-baidu/dist/uni.mp.esm.js b/packages/uni-mp-baidu/dist/uni.mp.esm.js index 8cc71a307..f9463c334 100644 --- a/packages/uni-mp-baidu/dist/uni.mp.esm.js +++ b/packages/uni-mp-baidu/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize } from '@vue/shared'; -import { injectHook, ref, nextTick, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, nextTick, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; // lifecycle // App and Page @@ -492,9 +492,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -509,7 +516,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index 47e33a858..afcf81f35 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -38,6 +38,7 @@ export { fixProperties, nextSetDataTick, initSetRef, + findRefValue, } from './runtime/util' // protocols diff --git a/packages/uni-mp-core/src/runtime/util.ts b/packages/uni-mp-core/src/runtime/util.ts index f4ac1a52d..a7500da16 100644 --- a/packages/uni-mp-core/src/runtime/util.ts +++ b/packages/uni-mp-core/src/runtime/util.ts @@ -5,6 +5,8 @@ import { ComponentPublicInstance, nextTick, } from 'vue' +// @ts-ignore +import { getExposeProxy } from 'vue' import { MPComponentInstance, MPComponentOptions } from './component' @@ -65,10 +67,18 @@ function selectAllComponents( const components = mpInstance.selectAllComponents(selector) components.forEach((component) => { const ref = component.properties.uR - $refs[ref] = component.$vm || component + $refs[ref] = findRefValue(component as MPComponentInstance) }) } +export function findRefValue(component: MPComponentInstance) { + const vm = component.$vm + if (vm) { + return getExposeProxy(vm.$) || vm + } + return component +} + export function initRefs( instance: ComponentInternalInstance, mpInstance: MPComponentInstance @@ -86,7 +96,7 @@ export function initRefs( if (!$refs[ref]) { $refs[ref] = [] } - $refs[ref].push(component.$vm || component) + $refs[ref].push(findRefValue(component as MPComponentInstance)) }) return $refs }, diff --git a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js index 17576d292..b7fce9afe 100644 --- a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize } from '@vue/shared'; -import { injectHook, ref, nextTick, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, nextTick, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; const ON_READY$1 = 'onReady'; @@ -487,9 +487,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -504,7 +511,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-mp-lark/dist/uni.mp.esm.js b/packages/uni-mp-lark/dist/uni.mp.esm.js index ae424fdee..c2b3891e9 100644 --- a/packages/uni-mp-lark/dist/uni.mp.esm.js +++ b/packages/uni-mp-lark/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize, isObject } from '@vue/shared'; -import { injectHook, ref, nextTick, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, nextTick, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; const ON_READY$1 = 'onReady'; @@ -486,9 +486,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -503,7 +510,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-mp-qq/dist/uni.mp.esm.js b/packages/uni-mp-qq/dist/uni.mp.esm.js index 19e71113a..42b551e67 100644 --- a/packages/uni-mp-qq/dist/uni.mp.esm.js +++ b/packages/uni-mp-qq/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize } from '@vue/shared'; -import { injectHook, ref, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; const ON_READY$1 = 'onReady'; @@ -483,9 +483,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -500,7 +507,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-mp-toutiao/dist/uni.mp.esm.js b/packages/uni-mp-toutiao/dist/uni.mp.esm.js index a703c43ec..cd6873b3c 100644 --- a/packages/uni-mp-toutiao/dist/uni.mp.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize, isObject } from '@vue/shared'; -import { injectHook, ref, nextTick, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, nextTick, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; const ON_READY$1 = 'onReady'; @@ -486,9 +486,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -503,7 +510,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index 2cd384b5b..cfa530808 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -4734,7 +4734,8 @@ function setRef$1(instance, isUnmount = false) { function findComponentPublicInstance(mpComponents, id) { const mpInstance = mpComponents.find(com => com && (com.properties || com.props).uI === id); if (mpInstance) { - return mpInstance.$vm; + const vm = mpInstance.$vm; + return getExposeProxy(vm.$) || vm; } return null; } @@ -5505,4 +5506,4 @@ function createApp(rootComponent, rootProps = null) { } const createSSRApp = createApp; -export { EffectScope, Fragment, ReactiveEffect, Text, c, callWithAsyncErrorHandling, callWithErrorHandling, computed, createApp, createSSRApp, createVNode$1 as createVNode, createVueApp, customRef, d, defineAsyncComponent, defineComponent, defineEmits, defineExpose, defineProps, diff, e, effect, effectScope, f, findComponentPropsData, getCurrentInstance, getCurrentScope, guardReactiveProps, h, inject, injectHook, invalidateJob, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, logError, markRaw, mergeDefaults, mergeProps, n, nextTick, o, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, p, patch, provide, proxyRefs, pruneComponentPropsCache, queuePostFlushCb, r, reactive, readonly, ref, resolveComponent, resolveDirective, resolveFilter, s, setCurrentRenderingInstance, setTemplateRef, setupDevtoolsPlugin, shallowReactive, shallowReadonly, shallowRef, sr, stop, t, toHandlers, toRaw, toRef, toRefs, triggerRef, unref, updateProps, useAttrs, useCssModule, useCssVars, useSSRContext, useSlots, version, w, warn$1 as warn, watch, watchEffect, watchPostEffect, watchSyncEffect, withAsyncContext, withCtx, withDefaults, withDirectives, withModifiers, withScopeId }; +export { EffectScope, Fragment, ReactiveEffect, Text, c, callWithAsyncErrorHandling, callWithErrorHandling, computed, createApp, createSSRApp, createVNode$1 as createVNode, createVueApp, customRef, d, defineAsyncComponent, defineComponent, defineEmits, defineExpose, defineProps, diff, e, effect, effectScope, f, findComponentPropsData, getCurrentInstance, getCurrentScope, getExposeProxy, guardReactiveProps, h, inject, injectHook, invalidateJob, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, logError, markRaw, mergeDefaults, mergeProps, n, nextTick, o, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, p, patch, provide, proxyRefs, pruneComponentPropsCache, queuePostFlushCb, r, reactive, readonly, ref, resolveComponent, resolveDirective, resolveFilter, s, setCurrentRenderingInstance, setTemplateRef, setupDevtoolsPlugin, shallowReactive, shallowReadonly, shallowRef, sr, stop, t, toHandlers, toRaw, toRef, toRefs, triggerRef, unref, updateProps, useAttrs, useCssModule, useCssVars, useSSRContext, useSlots, version, w, warn$1 as warn, watch, watchEffect, watchPostEffect, watchSyncEffect, withAsyncContext, withCtx, withDefaults, withDirectives, withModifiers, withScopeId }; diff --git a/packages/uni-mp-vue/lib/vue.runtime.esm.js b/packages/uni-mp-vue/lib/vue.runtime.esm.js index bba7b4b76..0e0f341fc 100644 --- a/packages/uni-mp-vue/lib/vue.runtime.esm.js +++ b/packages/uni-mp-vue/lib/vue.runtime.esm.js @@ -4734,7 +4734,8 @@ function setRef(instance, isUnmount = false) { function findComponentPublicInstance(mpComponents, id) { const mpInstance = mpComponents.find(com => com && (com.properties || com.props).uI === id); if (mpInstance) { - return mpInstance.$vm; + const vm = mpInstance.$vm; + return getExposeProxy(vm.$) || vm; } return null; } @@ -5071,4 +5072,4 @@ function initCssVarsRender(instance, getter) { function withModifiers() { } function createVNode$1() { } -export { EffectScope, Fragment, ReactiveEffect, Text, callWithAsyncErrorHandling, callWithErrorHandling, computed, createVNode$1 as createVNode, createVueApp, customRef, defineAsyncComponent, defineComponent, defineEmits, defineExpose, defineProps, diff, effect, effectScope, getCurrentInstance, getCurrentScope, guardReactiveProps, inject, injectHook, invalidateJob, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, logError, markRaw, mergeDefaults, mergeProps, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, patch, provide, proxyRefs, queuePostFlushCb, reactive, readonly, ref, resolveComponent, resolveDirective, resolveFilter, setCurrentRenderingInstance, setTemplateRef, shallowReactive, shallowReadonly, shallowRef, stop, toHandlers, toRaw, toRef, toRefs, triggerRef, unref, updateProps, useAttrs, useCssModule, useCssVars, useSSRContext, useSlots, version, warn$1 as warn, watch, watchEffect, watchPostEffect, watchSyncEffect, withAsyncContext, withCtx, withDefaults, withDirectives, withModifiers, withScopeId }; +export { EffectScope, Fragment, ReactiveEffect, Text, callWithAsyncErrorHandling, callWithErrorHandling, computed, createVNode$1 as createVNode, createVueApp, customRef, defineAsyncComponent, defineComponent, defineEmits, defineExpose, defineProps, diff, effect, effectScope, getCurrentInstance, getCurrentScope, getExposeProxy, guardReactiveProps, inject, injectHook, invalidateJob, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, logError, markRaw, mergeDefaults, mergeProps, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, patch, provide, proxyRefs, queuePostFlushCb, reactive, readonly, ref, resolveComponent, resolveDirective, resolveFilter, setCurrentRenderingInstance, setTemplateRef, shallowReactive, shallowReadonly, shallowRef, stop, toHandlers, toRaw, toRef, toRefs, triggerRef, unref, updateProps, useAttrs, useCssModule, useCssVars, useSSRContext, useSlots, version, warn$1 as warn, watch, watchEffect, watchPostEffect, watchSyncEffect, withAsyncContext, withCtx, withDefaults, withDirectives, withModifiers, withScopeId }; diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index 82d1b0e9e..30b514776 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize } from '@vue/shared'; -import { injectHook, ref, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; // quickapp-webview 不能使用 default 作为插槽名称 const SLOT_DEFAULT_NAME = 'd'; @@ -355,9 +355,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -372,7 +379,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, diff --git a/packages/uni-quickapp-webview/dist/uni.mp.esm.js b/packages/uni-quickapp-webview/dist/uni.mp.esm.js index 55d8e2b95..4fb38b805 100644 --- a/packages/uni-quickapp-webview/dist/uni.mp.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, hasOwn, isFunction, extend, camelize, isObject } from '@vue/shared'; -import { injectHook, ref, nextTick, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; +import { injectHook, ref, nextTick, getExposeProxy, findComponentPropsData, toRaw, updateProps, invalidateJob, pruneComponentPropsCache } from 'vue'; const ON_READY$1 = 'onReady'; @@ -483,9 +483,16 @@ function selectAllComponents(mpInstance, selector, $refs) { const components = mpInstance.selectAllComponents(selector); components.forEach((component) => { const ref = component.properties.uR; - $refs[ref] = component.$vm || component; + $refs[ref] = findRefValue(component); }); } +function findRefValue(component) { + const vm = component.$vm; + if (vm) { + return getExposeProxy(vm.$) || vm; + } + return component; +} function initRefs(instance, mpInstance) { Object.defineProperty(instance, 'refs', { get() { @@ -500,7 +507,7 @@ function initRefs(instance, mpInstance) { if (!$refs[ref]) { $refs[ref] = []; } - $refs[ref].push(component.$vm || component); + $refs[ref].push(findRefValue(component)); }); return $refs; }, -- GitLab