]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: optimize expose
authorEvan You <yyx990803@gmail.com>
Mon, 16 Nov 2020 16:28:37 +0000 (11:28 -0500)
committerEvan You <yyx990803@gmail.com>
Mon, 16 Nov 2020 16:28:37 +0000 (11:28 -0500)
packages/runtime-core/__tests__/apiExpose.spec.ts
packages/runtime-core/src/componentOptions.ts

index febf345240920f40afd1722954fe3677bd89e741..7695360be59831535d087c7187ef44909f861091 100644 (file)
@@ -95,4 +95,50 @@ describe('api: expose', () => {
     expect(childRef.value.bar).toBe(2)
     expect(childRef.value.baz).toBeUndefined()
   })
+
+  test('options: empty', () => {
+    const Child = defineComponent({
+      render() {},
+      expose: [],
+      data() {
+        return {
+          foo: 1
+        }
+      }
+    })
+
+    const childRef = ref()
+    const Parent = {
+      setup() {
+        return () => h(Child, { ref: childRef })
+      }
+    }
+    const root = nodeOps.createElement('div')
+    render(h(Parent), root)
+    expect(childRef.value).toBeTruthy()
+    expect('foo' in childRef.value).toBe(false)
+  })
+
+  test('options: empty + setup context', () => {
+    const Child = defineComponent({
+      render() {},
+      expose: [],
+      setup(_, { expose }) {
+        expose({
+          foo: 1
+        })
+      }
+    })
+
+    const childRef = ref()
+    const Parent = {
+      setup() {
+        return () => h(Child, { ref: childRef })
+      }
+    }
+    const root = nodeOps.createElement('div')
+    render(h(Parent), root)
+    expect(childRef.value).toBeTruthy()
+    expect(childRef.value.foo).toBe(1)
+  })
 })
index 8a158ee60a837b9a298ef71e47be6adc862679dc..dd95e29ad76dbe1cfac604c5461ac3d1ce6d62bd 100644 (file)
@@ -743,11 +743,19 @@ export function applyOptions(
     onUnmounted(unmounted.bind(publicThis))
   }
 
-  if (!asMixin && expose) {
-    const exposed = instance.exposed || (instance.exposed = proxyRefs({}))
-    expose.forEach(key => {
-      exposed[key] = toRef(publicThis, key as any)
-    })
+  if (isArray(expose)) {
+    if (!asMixin) {
+      if (expose.length) {
+        const exposed = instance.exposed || (instance.exposed = proxyRefs({}))
+        expose.forEach(key => {
+          exposed[key] = toRef(publicThis, key as any)
+        })
+      } else if (!instance.exposed) {
+        instance.exposed = EMPTY_OBJ
+      }
+    } else if (__DEV__) {
+      warn(`The \`expose\` option is ignored when used in mixins.`)
+    }
   }
 }