From 215f877d1b7eb6583f7adf15676ead8611f07379 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 12 Mar 2018 13:05:31 -0400 Subject: [PATCH] fix(keep-alive): run prune after render for correct active component check fix #7566 --- src/core/components/keep-alive.js | 10 +++---- .../component/component-keep-alive.spec.js | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/core/components/keep-alive.js b/src/core/components/keep-alive.js index 53dd264aaa1..fb4cf1e883b 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -71,13 +71,13 @@ export default { } }, - watch: { - include (val: string | RegExp | Array) { + mounted () { + this.$watch('include', val => { pruneCache(this, name => matches(val, name)) - }, - exclude (val: string | RegExp | Array) { + }) + this.$watch('exclude', val => { pruneCache(this, name => !matches(val, name)) - } + }) }, render () { diff --git a/test/unit/features/component/component-keep-alive.spec.js b/test/unit/features/component/component-keep-alive.spec.js index b89fbdc9d65..49f7dd60d71 100644 --- a/test/unit/features/component/component-keep-alive.spec.js +++ b/test/unit/features/component/component-keep-alive.spec.js @@ -393,6 +393,35 @@ describe('Component keep-alive', () => { }).then(done) }) + it('prune cache on include/exclude change + view switch', done => { + const vm = new Vue({ + template: ` +
+ + + +
+ `, + data: { + view: 'one', + include: 'one,two' + }, + components + }).$mount() + + vm.view = 'two' + waitForUpdate(() => { + assertHookCalls(one, [1, 1, 1, 1, 0]) + assertHookCalls(two, [1, 1, 1, 0, 0]) + vm.include = 'one' + vm.view = 'one' + }).then(() => { + assertHookCalls(one, [1, 1, 2, 1, 0]) + // two should be pruned + assertHookCalls(two, [1, 1, 1, 1, 1]) + }).then(done) + }) + it('should not prune currently active instance', done => { const vm = new Vue({ template: `