Skip to content

Commit

Permalink
adjust named slot resolve check (fix vuejs#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 30, 2016
1 parent b8369e8 commit 99ea0f8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ declare interface Component {
_isVue: true;
_self: Component;
_renderProxy: Component;
_renderParent: ?Component;
_renderContext: ?Component;
_watcher: Watcher;
_watchers: Array<Watcher>;
_data: Object;
Expand Down
2 changes: 1 addition & 1 deletion src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
}
// resolve slots + force update if has children
if (hasChildren) {
vm.$slots = resolveSlots(renderChildren)
vm.$slots = resolveSlots(renderChildren, vm._renderContext)
vm.$forceUpdate()
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/core/instance/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function initRender (vm: Component) {
vm.$vnode = null // the placeholder node in parent tree
vm._vnode = null // the root of the child tree
vm._staticTrees = null
vm.$slots = resolveSlots(vm.$options._renderChildren)
vm._renderContext = vm.$options._parentVnode && vm.$options._parentVnode.context
vm.$slots = resolveSlots(vm.$options._renderChildren, vm._renderContext)
// bind the public createElement fn to this instance
// so that we get proper render context inside it.
vm.$createElement = bind(createElement, vm)
Expand Down Expand Up @@ -215,7 +216,8 @@ export function renderMixin (Vue: Class<Component>) {
}

export function resolveSlots (
renderChildren: ?VNodeChildren
renderChildren: ?VNodeChildren,
context: ?Component
): { [key: string]: Array<VNode> } {
const slots = {}
if (!renderChildren) {
Expand All @@ -226,8 +228,10 @@ export function resolveSlots (
let name, child
for (let i = 0, l = children.length; i < l; i++) {
child = children[i]
if (child.data && (name = child.data.slot)) {
delete child.data.slot
// named slots should only be respected if the vnode was rendered in the
// same context.
if (child.context === context &&
child.data && (name = child.data.slot)) {
const slot = (slots[name] || (slots[name] = []))
if (child.tag === 'template') {
slot.push.apply(slot, child.children)
Expand Down
7 changes: 4 additions & 3 deletions src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import VNode from './vnode'
import { normalizeChildren } from './helpers'
import { activeInstance, callHook } from '../instance/lifecycle'
import { resolveSlots } from '../instance/render'
import { warn, isObject, hasOwn, hyphenate, validateProp } from '../util/index'
import { createElement } from './create-element'
import { warn, isObject, hasOwn, hyphenate, validateProp, bind } from '../util/index'

const hooks = { init, prepatch, insert, destroy }
const hooksToMerge = Object.keys(hooks)
Expand Down Expand Up @@ -101,13 +102,13 @@ function createFunctionalComponent (
}
return Ctor.options.render.call(
null,
context.$createElement,
bind(createElement, { _self: Object.create(context) }),
{
props,
data,
parent: context,
children: normalizeChildren(children),
slots: () => resolveSlots(children)
slots: () => resolveSlots(children, context)
}
)
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/features/component/component-slot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ describe('Component slot', () => {
</div>
`
}

const vm = new Vue({
template: '<test><span slot="foo">foo</span></test>',
components: {
Expand Down

0 comments on commit 99ea0f8

Please sign in to comment.