Skip to content

Commit 970eac9

Browse files
committed
add compileTemplateForSlots()
1 parent 1a9dc0a commit 970eac9

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

packages/create-instance/create-instance.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import { componentNeedsCompiling } from 'shared/validators'
1212
import { validateSlots } from './validate-slots'
1313
import createScopedSlots from './create-scoped-slots'
1414

15+
function compileTemplateForSlots (slots: Object): void {
16+
Object.keys(slots).forEach(key => {
17+
const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]]
18+
slot.forEach(slotValue => {
19+
if (componentNeedsCompiling(slotValue)) {
20+
compileTemplate(slotValue)
21+
}
22+
})
23+
})
24+
}
25+
1526
export default function createInstance (
1627
component: Component,
1728
options: Options,
@@ -109,6 +120,8 @@ export default function createInstance (
109120
})
110121

111122
if (options.slots) {
123+
compileTemplateForSlots(options.slots)
124+
// $FlowIgnore
112125
validateSlots(options.slots)
113126
}
114127

test/specs/mounting-options/slots.spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,10 @@ describeWithMountingMethods('options.slots', mountingMethod => {
575575
mountingMethod.name === 'renderToString',
576576
'sets a component which can access the parent component and the child component',
577577
() => {
578+
const childComponentName = 'component-with-parent-name'
578579
const localVue = createLocalVue()
579580
localVue.prototype.bar = 'FOO'
580-
const ParentComponent = mount(
581+
let ParentComponent = mount(
581582
{
582583
name: 'parentComponent',
583584
template: '<div><slot /></div>',
@@ -600,11 +601,37 @@ describeWithMountingMethods('options.slots', mountingMethod => {
600601
localVue
601602
}
602603
)
603-
const childComponentName = 'component-with-parent-name'
604604
expect(ParentComponent.vm.childComponentName).to.equal(childComponentName)
605605
expect(ParentComponent.vm.$children.length).to.equal(2)
606606
expect(ParentComponent.vm.$children.every(c => c.$options.name === childComponentName)).to.equal(true)
607607
expect(ParentComponent.html()).to.equal('<div><div><span baz="qux">FOO,quux</span></div><div><span baz="qux">FOO,quux</span></div></div>')
608+
609+
ParentComponent = mount(
610+
{
611+
name: 'parentComponent',
612+
template: '<div><slot /></div>',
613+
data () {
614+
return {
615+
childComponentName: ''
616+
}
617+
}
618+
},
619+
{
620+
slots: {
621+
default: {
622+
name: childComponentName,
623+
template: '<p>1234</p>',
624+
mounted () {
625+
this.$parent.childComponentName = this.$options.name
626+
}
627+
}
628+
}
629+
}
630+
)
631+
expect(ParentComponent.vm.childComponentName).to.equal(childComponentName)
632+
expect(ParentComponent.vm.$children.length).to.equal(1)
633+
expect(ParentComponent.vm.$children.every(c => c.$options.name === childComponentName)).to.equal(true)
634+
expect(ParentComponent.html()).to.equal('<div><p>1234</p></div>')
608635
}
609636
)
610637
})

0 commit comments

Comments
 (0)