Skip to content

Commit 978d952

Browse files
committed
fix(runtime-core): fix scopeId inheritance for component inside slots
1 parent 0dd5cde commit 978d952

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/runtime-core/__tests__/helpers/scopeId.spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ describe('scopeId runtime support', () => {
4545
return h('div', this.$slots.default())
4646
})
4747
}
48+
const withChil2Id = withScopeId('child2')
49+
const Child2 = {
50+
__scopeId: 'child2',
51+
render: withChil2Id(() => h('span'))
52+
}
4853
const App = {
4954
__scopeId: 'parent',
5055
render: withParentId(() => {
5156
return h(
5257
Child,
5358
withParentId(() => {
54-
return h('div')
59+
return [h('div'), h(Child2)]
5560
})
5661
)
5762
})
@@ -62,7 +67,14 @@ describe('scopeId runtime support', () => {
6267
// - scopeId from parent
6368
// - slotted scopeId (with `-s` postfix) from child (the tree owner)
6469
expect(serializeInner(root)).toBe(
65-
`<div parent child><div parent child-s></div></div>`
70+
`<div parent child>` +
71+
`<div parent child-s></div>` +
72+
// component inside slot should have:
73+
// - scopeId from template context
74+
// - slotted scopeId from slot owner
75+
// - its own scopeId
76+
`<span parent child-s child2></span>` +
77+
`</div>`
6678
)
6779
})
6880
})

packages/runtime-core/src/componentRenderUtils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function renderComponentRoot(
4242
): VNode {
4343
const {
4444
type: Component,
45+
parent,
4546
vnode,
4647
proxy,
4748
withProxy,
@@ -148,9 +149,15 @@ export function renderComponentRoot(
148149
}
149150

150151
// inherit scopeId
151-
if (vnode.scopeId) {
152-
root = cloneVNode(root, { [vnode.scopeId]: '' })
152+
const scopeId = vnode.scopeId
153+
if (scopeId) {
154+
root = cloneVNode(root, { [scopeId]: '' })
153155
}
156+
const treeOwnerId = parent && parent.type.__scopeId
157+
if (treeOwnerId && treeOwnerId !== scopeId) {
158+
root = cloneVNode(root, { [treeOwnerId + '-s']: '' })
159+
}
160+
154161
// inherit directives
155162
if (vnode.dirs) {
156163
if (__DEV__ && !isElementRoot(root)) {

0 commit comments

Comments
 (0)