瀏覽代碼

fix(runtime-vapor): correctly check slot existence in ownKeys (#14252)

Denis 3 月之前
父節點
當前提交
1d376e06b6
共有 2 個文件被更改,包括 46 次插入4 次删除
  1. 40 0
      packages/runtime-vapor/__tests__/componentSlots.spec.ts
  2. 6 4
      packages/runtime-vapor/src/componentSlots.ts

+ 40 - 0
packages/runtime-vapor/__tests__/componentSlots.spec.ts

@@ -527,6 +527,46 @@ describe('component: slots', () => {
       expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
     })
 
+    test('slots proxy ownKeys trap correctly reflects dynamic slot presence', async () => {
+      const val = ref('header')
+      const toggle = ref(false)
+
+      let instance: any
+      const Comp = defineVaporComponent(() => {
+        instance = currentInstance
+        const n0 = template('<div></div>')()
+        prepend(n0 as any as ParentNode, createSlot('header', null))
+        return n0
+      })
+
+      define(() => {
+        // dynamic slot
+        return createComponent(Comp, null, {
+          $: [
+            () =>
+              (toggle.value
+                ? {
+                    name: val.value,
+                    fn: () => {
+                      return template('<h1></h1>')()
+                    },
+                  }
+                : void 0) as DynamicSlot,
+          ],
+        })
+      }).render()
+
+      expect(Reflect.ownKeys(instance.slots)).not.toContain('header')
+
+      toggle.value = true
+      await nextTick()
+      expect(Reflect.ownKeys(instance.slots)).toContain('header')
+
+      toggle.value = false
+      await nextTick()
+      expect(Reflect.ownKeys(instance.slots)).not.toContain('header')
+    })
+
     test('render fallback when slot content is not valid', async () => {
       const Child = {
         setup() {

+ 6 - 4
packages/runtime-vapor/src/componentSlots.ts

@@ -75,10 +75,12 @@ export const dynamicSlotsProxyHandlers: ProxyHandler<RawSlots> = {
       for (const source of dynamicSources) {
         if (isFunction(source)) {
           const slot = resolveFunctionSource(source)
-          if (isArray(slot)) {
-            for (const s of slot) keys.push(String(s.name))
-          } else {
-            keys.push(String(slot.name))
+          if (slot) {
+            if (isArray(slot)) {
+              for (const s of slot) keys.push(String(s.name))
+            } else {
+              keys.push(String(slot.name))
+            }
           }
         } else {
           keys.push(...Object.keys(source))