From b182c00a020ab3f1e9aa386415b6081d1d263a0a Mon Sep 17 00:00:00 2001 From: jasmineMu <389549151@qq.com> Date: Mon, 13 May 2019 15:26:40 +0800 Subject: [PATCH 1/2] Update keep-alive.js The same component(keep-alive) is created many times when max is set --- src/core/components/keep-alive.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/components/keep-alive.js b/src/core/components/keep-alive.js index fb4cf1e883b..e9ce1e8957d 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -113,7 +113,7 @@ export default { keys.push(key) // prune oldest entry if (this.max && keys.length > parseInt(this.max)) { - pruneCacheEntry(cache, keys[0], keys, this._vnode) + pruneCacheEntry(cache, keys[0], keys, vnode) } } From b79718d0a3a0ab7572c2cdaeceb1e4e682a7d8ce Mon Sep 17 00:00:00 2001 From: muliqun Date: Tue, 14 May 2019 14:11:55 +0800 Subject: [PATCH 2/2] fix(#10015 keep-alive): bug prune oldest entry a unit test --- .../component/component-keep-alive.spec.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/unit/features/component/component-keep-alive.spec.js b/test/unit/features/component/component-keep-alive.spec.js index a2cdf6a4f09..269c8317b53 100644 --- a/test/unit/features/component/component-keep-alive.spec.js +++ b/test/unit/features/component/component-keep-alive.spec.js @@ -572,6 +572,56 @@ describe('Component keep-alive', () => { }).then(done) }) + // #10015 + it('prune cache on max set 1', done => { + const spyA = jasmine.createSpy() + const spyB = jasmine.createSpy() + const spyAD = jasmine.createSpy() + const spyBD = jasmine.createSpy() + + function assertCount (calls) { + expect([ + spyA.calls.count(), + spyAD.calls.count(), + spyB.calls.count(), + spyBD.calls.count(), + ]).toEqual(calls) + } + + const vm = new Vue({ + template: ` + + + + `, + data: { + n: 'aa' + }, + components: { + aa: { + template: '
a
', + created: spyA, + destroyed: spyAD + }, + bb: { + template: '
bbb
', + created: spyB, + destroyed: spyBD + } + } + }).$mount() + + assertCount([1, 0, 0, 0]) + vm.n = 'bb' + waitForUpdate(() => { + // should prune A because max cache reached + assertCount([1, 1, 1, 0]) + vm.n = 'aa' + }).then(() => { + assertCount([2, 1, 1, 1]) + }).then(done) + }) + it('should warn unknown component inside', () => { new Vue({ template: ``