Skip to content

Commit 90a455c

Browse files
committed
fix replaced component root nodes losing parent scopeId (fix #4774)
1 parent 379695c commit 90a455c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Diff for: src/core/vdom/patch.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,14 @@ export function createPatchFunction (backend) {
260260
// of going through the normal attribute patching process.
261261
function setScope (vnode) {
262262
let i
263-
if (isDef(i = vnode.context) && isDef(i = i.$options._scopeId)) {
264-
nodeOps.setAttribute(vnode.elm, i, '')
263+
let ancestor = vnode
264+
while (ancestor) {
265+
if (isDef(i = ancestor.context) && isDef(i = i.$options._scopeId)) {
266+
nodeOps.setAttribute(vnode.elm, i, '')
267+
}
268+
ancestor = ancestor.parent
265269
}
270+
// for slot content they should also get the scopeId from the host instance.
266271
if (isDef(i = activeInstance) &&
267272
i !== vnode.context &&
268273
isDef(i = i.$options._scopeId)) {

Diff for: test/unit/features/options/_scopeId.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,32 @@ describe('Options _scopeId', () => {
4040
expect(vm.$el.children[0].children[0].hasAttribute('foo')).toBe(true)
4141
expect(vm.$el.children[0].children[0].hasAttribute('bar')).toBe(true)
4242
})
43+
44+
// #4774
45+
it('should not discard parent scopeId when component root element is replaced', done => {
46+
const vm = new Vue({
47+
_scopeId: 'data-1',
48+
template: `<div><child ref="child" /></div>`,
49+
components: {
50+
child: {
51+
_scopeId: 'data-2',
52+
data: () => ({ show: true }),
53+
template: '<div v-if="show"></div>'
54+
}
55+
}
56+
}).$mount()
57+
58+
const child = vm.$refs.child
59+
60+
expect(child.$el.hasAttribute('data-1')).toBe(true)
61+
expect(child.$el.hasAttribute('data-2')).toBe(true)
62+
63+
child.show = false
64+
waitForUpdate(() => {
65+
child.show = true
66+
}).then(() => {
67+
expect(child.$el.hasAttribute('data-1')).toBe(true)
68+
expect(child.$el.hasAttribute('data-2')).toBe(true)
69+
}).then(done)
70+
})
4371
})

0 commit comments

Comments
 (0)