Răsfoiți Sursa

fix component in slot lifecycle (fix #3437)

Evan You 9 ani în urmă
părinte
comite
9d43bff27e

+ 1 - 0
src/core/vdom/create-component.js

@@ -161,6 +161,7 @@ function destroy (vnode: MountedComponentVNode) {
   if (!vnode.child._isDestroyed) {
     if (!vnode.data.keepAlive) {
       vnode.child.$destroy()
+      vnode.child = null
     } else {
       vnode.child._inactive = true
       callHook(vnode.child, 'deactivated')

+ 43 - 0
test/unit/features/component/component-slot.spec.js

@@ -411,4 +411,47 @@ describe('Component slot', () => {
       expect(vm.$el.textContent).toBe('foo2')
     }).then(done)
   })
+
+  // #3437
+  it('should correctly re-create components in slot', done => {
+    const calls = []
+    const vm = new Vue({
+      template: `
+        <comp ref="child">
+          <div slot="foo">
+            <child></child>
+          </div>
+        </comp>
+      `,
+      components: {
+        comp: {
+          data () {
+            return { ok: true }
+          },
+          template: `<div><slot name="foo" v-if="ok"></slot></div>`
+        },
+        child: {
+          template: '<div>child</div>',
+          created () {
+            calls.push(1)
+          },
+          destroyed () {
+            calls.push(2)
+          }
+        }
+      }
+    }).$mount()
+
+    expect(calls).toEqual([1])
+    vm.$refs.child.ok = false
+    waitForUpdate(() => {
+      expect(calls).toEqual([1, 2])
+      vm.$refs.child.ok = true
+    }).then(() => {
+      expect(calls).toEqual([1, 2, 1])
+      vm.$refs.child.ok = false
+    }).then(() => {
+      expect(calls).toEqual([1, 2, 1, 2])
+    }).then(done)
+  })
 })