Kaynağa Gözat

fix(keep-alive): should not destroy active instance when pruning cache

fix #7105
Evan You 8 yıl önce
ebeveyn
işleme
3932a451a1

+ 1 - 1
src/core/components/keep-alive.js

@@ -41,7 +41,7 @@ function pruneCacheEntry (
   current?: VNode
 ) {
   const cached = cache[key]
-  if (cached && cached !== current) {
+  if (cached && (!current || cached.tag !== current.tag)) {
     cached.componentInstance.$destroy()
   }
   cache[key] = null

+ 28 - 0
test/unit/features/component/component-keep-alive.spec.js

@@ -656,6 +656,34 @@ describe('Component keep-alive', () => {
     }).then(done)
   })
 
+  // #7105
+  it('should not destroy active instance when pruning cache', done => {
+    const Foo = {
+      template: `<div>foo</div>`,
+      destroyed: jasmine.createSpy('destroyed')
+    }
+    const vm = new Vue({
+      template: `
+        <div>
+          <keep-alive :include="include">
+            <foo/>
+          </keep-alive>
+        </div>
+      `,
+      data: {
+        include: ['foo']
+      },
+      components: { Foo }
+    }).$mount()
+    // condition: a render where a previous component is reused
+    vm.include = ['foo']
+    waitForUpdate(() => {
+      vm.include = ['']
+    }).then(() => {
+      expect(Foo.destroyed).not.toHaveBeenCalled()
+    }).then(done)
+  })
+
   if (!isIE9) {
     it('with transition-mode out-in', done => {
       let next