Skip to content

Commit 9edb358

Browse files
committed
should fallback on default slot if no content is left (vuejs#1965)
1 parent 746e17a commit 9edb358

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/directives/element/slot.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ export default {
3535
// Default content
3636
var self = this
3737
var compileDefaultContent = function () {
38-
self.compile(
39-
extractFragment(raw.childNodes, raw, true),
40-
context,
41-
host
42-
)
38+
var content = extractFragment(raw.childNodes, raw, true)
39+
if (content.hasChildNodes()) {
40+
self.compile(content, context, host)
41+
} else {
42+
self.fallback()
43+
}
4344
}
4445
if (!host._isCompiled) {
4546
// defer until the end of instance compilation,

test/unit/specs/directives/element/slot_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ describe('Slot Distribution', function () {
5959
expect(el.lastChild.textContent).toBe('slot b')
6060
})
6161

62+
it('fallback content with mixed named/unnamed slots', function () {
63+
el.innerHTML = '<p slot="b">slot b</p>'
64+
options.template =
65+
'<slot><p>fallback a</p></slot>' +
66+
'<slot name="b">fallback b</slot>'
67+
mount()
68+
expect(el.childNodes.length).toBe(2)
69+
expect(el.firstChild.textContent).toBe('fallback a')
70+
expect(el.lastChild.textContent).toBe('slot b')
71+
})
72+
6273
it('selector matching multiple elements', function () {
6374
el.innerHTML = '<p slot="t">1</p><div></div><p slot="t">2</p>'
6475
options.template = '<slot name="t"></slot>'

0 commit comments

Comments
 (0)