Skip to content

Commit

Permalink
refactor: adjust #7941 for slots unification
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 9, 2019
1 parent fb6aa06 commit 23a1459
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/core/vdom/create-functional-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ export function FunctionalRenderContext (
this.children = children
this.parent = parent
this.listeners = data.on || emptyObject
this.scopedSlots = data.scopedSlots || emptyObject
this.injections = resolveInject(options.inject, parent)
this.slots = () => resolveSlots(children, parent)

Object.defineProperty(this, 'scopedSlots', {
enumerable: true,
get () {
return normalizeScopedSlots(data.scopedSlots, this.slots())
}
})

// support for compiled functional template
if (isCompiled) {
// exposing $options for renderStatic()
Expand Down
1 change: 1 addition & 0 deletions src/core/vdom/helpers/normalize-scoped-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function normalizeScopedSlots (
}
res._normalized = true
}
// expose normal slots on scopedSlots
if (normalSlots !== emptyObject) {
for (const key in normalSlots) {
res[key] = () => normalSlots[key]
Expand Down
16 changes: 10 additions & 6 deletions test/unit/features/options/functional.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,25 @@ describe('Options functional', () => {
document.body.removeChild(vm.$el)
})

it('should expose data.scopedSlots as scopedSlots', () => {
it('should expose scopedSlots on render context', () => {
const vm = new Vue({
template: '<div><wrap><p slot-scope="a">{{ a }}</p></wrap></div>',
template: '<div><wrap>foo<p slot="p" slot-scope="a">{{ a }}</p></wrap></div>',
components: {
wrap: {
functional: true,
render (h, { scopedSlots, data }) {
expect(data.scopedSlots).toBe(scopedSlots)
return data.scopedSlots.default('a')
render (h, { scopedSlots }) {
return [
// scoped
scopedSlots.p('a'),
// normal slot content should be exposed as well
scopedSlots.default()
]
}
}
}
}).$mount()

expect(vm.$el.textContent).toBe('a')
expect(vm.$el.textContent).toBe('afoo')
})

it('should support returning more than one root node', () => {
Expand Down

0 comments on commit 23a1459

Please sign in to comment.