فهرست منبع

fix: empty scoped slot should return undefined

fix #9452
Evan You 7 سال پیش
والد
کامیت
57bc80a546
2فایلهای تغییر یافته به همراه30 افزوده شده و 2 حذف شده
  1. 5 2
      src/core/vdom/helpers/normalize-scoped-slots.js
  2. 25 0
      test/unit/features/component/component-scoped-slot.spec.js

+ 5 - 2
src/core/vdom/helpers/normalize-scoped-slots.js

@@ -33,10 +33,13 @@ export function normalizeScopedSlots (
 
 function normalizeScopedSlot(fn: Function): Function {
   return scope => {
-    const res = fn(scope)
-    return res && typeof res === 'object' && !Array.isArray(res)
+    let res = fn(scope)
+    res = res && typeof res === 'object' && !Array.isArray(res)
       ? [res] // single vnode
       : normalizeChildren(res)
+    return res && res.length === 0
+      ? undefined
+      : res
   }
 }
 

+ 25 - 0
test/unit/features/component/component-scoped-slot.spec.js

@@ -1034,4 +1034,29 @@ describe('Component scoped slot', () => {
       expect(vm.$el.textContent).toBe(JSON.stringify({ changed: 'hello' }, null, 2))
     }).then(done)
   })
+
+  // #9452
+  it('fallback for scoped slots passed multiple levels down', () => {
+    const inner = {
+      template: `<div><slot>fallback</slot></div>`
+    }
+
+    const wrapper = {
+      template: `
+        <inner>
+          <template #default>
+            <slot/>
+          </template>
+        </inner>
+      `,
+      components: { inner }
+    }
+
+    const vm = new Vue({
+      components: { wrapper, inner },
+      template: `<wrapper/>`
+    }).$mount()
+
+    expect(vm.$el.textContent).toBe(`fallback`)
+  })
 })