Skip to content

Commit 99ea0f8

Browse files
committed
adjust named slot resolve check (fix #3819)
1 parent b8369e8 commit 99ea0f8

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

flow/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ declare interface Component {
5252
_isVue: true;
5353
_self: Component;
5454
_renderProxy: Component;
55-
_renderParent: ?Component;
55+
_renderContext: ?Component;
5656
_watcher: Watcher;
5757
_watchers: Array<Watcher>;
5858
_data: Object;

src/core/instance/lifecycle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
141141
}
142142
// resolve slots + force update if has children
143143
if (hasChildren) {
144-
vm.$slots = resolveSlots(renderChildren)
144+
vm.$slots = resolveSlots(renderChildren, vm._renderContext)
145145
vm.$forceUpdate()
146146
}
147147
}

src/core/instance/render.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export function initRender (vm: Component) {
1414
vm.$vnode = null // the placeholder node in parent tree
1515
vm._vnode = null // the root of the child tree
1616
vm._staticTrees = null
17-
vm.$slots = resolveSlots(vm.$options._renderChildren)
17+
vm._renderContext = vm.$options._parentVnode && vm.$options._parentVnode.context
18+
vm.$slots = resolveSlots(vm.$options._renderChildren, vm._renderContext)
1819
// bind the public createElement fn to this instance
1920
// so that we get proper render context inside it.
2021
vm.$createElement = bind(createElement, vm)
@@ -215,7 +216,8 @@ export function renderMixin (Vue: Class<Component>) {
215216
}
216217

217218
export function resolveSlots (
218-
renderChildren: ?VNodeChildren
219+
renderChildren: ?VNodeChildren,
220+
context: ?Component
219221
): { [key: string]: Array<VNode> } {
220222
const slots = {}
221223
if (!renderChildren) {
@@ -226,8 +228,10 @@ export function resolveSlots (
226228
let name, child
227229
for (let i = 0, l = children.length; i < l; i++) {
228230
child = children[i]
229-
if (child.data && (name = child.data.slot)) {
230-
delete child.data.slot
231+
// named slots should only be respected if the vnode was rendered in the
232+
// same context.
233+
if (child.context === context &&
234+
child.data && (name = child.data.slot)) {
231235
const slot = (slots[name] || (slots[name] = []))
232236
if (child.tag === 'template') {
233237
slot.push.apply(slot, child.children)

src/core/vdom/create-component.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import VNode from './vnode'
55
import { normalizeChildren } from './helpers'
66
import { activeInstance, callHook } from '../instance/lifecycle'
77
import { resolveSlots } from '../instance/render'
8-
import { warn, isObject, hasOwn, hyphenate, validateProp } from '../util/index'
8+
import { createElement } from './create-element'
9+
import { warn, isObject, hasOwn, hyphenate, validateProp, bind } from '../util/index'
910

1011
const hooks = { init, prepatch, insert, destroy }
1112
const hooksToMerge = Object.keys(hooks)
@@ -101,13 +102,13 @@ function createFunctionalComponent (
101102
}
102103
return Ctor.options.render.call(
103104
null,
104-
context.$createElement,
105+
bind(createElement, { _self: Object.create(context) }),
105106
{
106107
props,
107108
data,
108109
parent: context,
109110
children: normalizeChildren(children),
110-
slots: () => resolveSlots(children)
111+
slots: () => resolveSlots(children, context)
111112
}
112113
)
113114
}

test/unit/features/component/component-slot.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ describe('Component slot', () => {
372372
</div>
373373
`
374374
}
375+
375376
const vm = new Vue({
376377
template: '<test><span slot="foo">foo</span></test>',
377378
components: {

0 commit comments

Comments
 (0)