Skip to content

Commit 9d43bff

Browse files
committed
fix component in slot lifecycle (fix #3437)
1 parent 63fe7fc commit 9d43bff

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/core/vdom/create-component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ function destroy (vnode: MountedComponentVNode) {
161161
if (!vnode.child._isDestroyed) {
162162
if (!vnode.data.keepAlive) {
163163
vnode.child.$destroy()
164+
vnode.child = null
164165
} else {
165166
vnode.child._inactive = true
166167
callHook(vnode.child, 'deactivated')

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,47 @@ describe('Component slot', () => {
411411
expect(vm.$el.textContent).toBe('foo2')
412412
}).then(done)
413413
})
414+
415+
// #3437
416+
it('should correctly re-create components in slot', done => {
417+
const calls = []
418+
const vm = new Vue({
419+
template: `
420+
<comp ref="child">
421+
<div slot="foo">
422+
<child></child>
423+
</div>
424+
</comp>
425+
`,
426+
components: {
427+
comp: {
428+
data () {
429+
return { ok: true }
430+
},
431+
template: `<div><slot name="foo" v-if="ok"></slot></div>`
432+
},
433+
child: {
434+
template: '<div>child</div>',
435+
created () {
436+
calls.push(1)
437+
},
438+
destroyed () {
439+
calls.push(2)
440+
}
441+
}
442+
}
443+
}).$mount()
444+
445+
expect(calls).toEqual([1])
446+
vm.$refs.child.ok = false
447+
waitForUpdate(() => {
448+
expect(calls).toEqual([1, 2])
449+
vm.$refs.child.ok = true
450+
}).then(() => {
451+
expect(calls).toEqual([1, 2, 1])
452+
vm.$refs.child.ok = false
453+
}).then(() => {
454+
expect(calls).toEqual([1, 2, 1, 2])
455+
}).then(done)
456+
})
414457
})

0 commit comments

Comments
 (0)