Просмотр исходного кода

fix(runtime-vapor): guard default slot for createPlainElement (#14422)

zhiyuanzmj 2 месяцев назад
Родитель
Сommit
6a6494190c

+ 13 - 0
packages/runtime-vapor/__tests__/customElement.spec.ts

@@ -2124,4 +2124,17 @@ describe('defineVaporCustomElement', () => {
       name: 'Foo',
     })
   })
+
+  test('inherit slots', () => {
+    const Comp = defineVaporCustomElement({
+      setup(props, { slots }) {
+        return createPlainElement('a', props, slots)
+      },
+    })
+    customElements.define('my-comp', Comp)
+    container.innerHTML = `<my-comp><my-comp>`
+    const comp = container.childNodes[0] as VaporElement
+    const consumer = comp.shadowRoot!.childNodes[0] as VaporElement
+    expect(consumer.tagName).toBe('A')
+  })
 })

+ 5 - 2
packages/runtime-vapor/src/component.ts

@@ -834,8 +834,11 @@ export function createPlainElement(
       renderEffect(() => frag.update(getSlot(rawSlots as RawSlots, 'default')))
       if (!isHydrating) insert(frag, el)
     } else {
-      const block = getSlot(rawSlots as RawSlots, 'default')!()
-      if (!isHydrating) insert(block, el)
+      let slot = getSlot(rawSlots as RawSlots, 'default')
+      if (slot) {
+        const block = slot()
+        if (!isHydrating) insert(block, el)
+      }
     }
     if (isHydrating) {
       setCurrentHydrationNode(nextNode)