Skip to content

Commit 184ff7d

Browse files
committed
fix slot fallback content scope inside v-for (fix #1282)
1 parent 1c166b7 commit 184ff7d

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/element-directives/slot.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ module.exports = {
8181

8282
compile: function (content, context, host) {
8383
if (content && context) {
84+
var scope = host
85+
? host._scope
86+
: this._scope
8487
this.unlink = context.$compile(
85-
content, host, this.vm._scope, this._frag
88+
content, host, scope, this._frag
8689
)
8790
}
8891
if (content) {

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ describe('Slot Distribution', function () {
77
beforeEach(function () {
88
el = document.createElement('div')
99
options = {
10-
el: el
10+
el: el,
11+
data: {
12+
msg: 'self'
13+
}
1114
}
1215
})
1316

@@ -39,10 +42,10 @@ describe('Slot Distribution', function () {
3942
})
4043

4144
it('fallback content', function () {
42-
options.template = '<slot><p>fallback</p></slot>'
45+
options.template = '<slot><p>{{msg}}</p></slot>'
4346
mount()
4447
expect(el.firstChild.tagName).toBe('P')
45-
expect(el.firstChild.textContent).toBe('fallback')
48+
expect(el.firstChild.textContent).toBe('self')
4649
})
4750

4851
it('fallback content with multiple named slots', function () {
@@ -363,4 +366,22 @@ describe('Slot Distribution', function () {
363366
expect(el.textContent).toBe('123234')
364367
})
365368

369+
it('fallback inside v-for', function () {
370+
new Vue({
371+
el: el,
372+
template: '<div v-for="n in 3"><comp></comp></div>',
373+
components: {
374+
comp: {
375+
template: '<div><slot>{{something}}</slot></div>',
376+
data: function () {
377+
return {
378+
something: 'hi'
379+
}
380+
}
381+
}
382+
}
383+
})
384+
expect(el.textContent).toBe('hihihi')
385+
})
386+
366387
})

0 commit comments

Comments
 (0)