From 938050808e462f48248d611eaa9befae40771bfe Mon Sep 17 00:00:00 2001 From: jkzing Date: Mon, 19 Jun 2017 22:50:53 +0800 Subject: [PATCH 1/5] allow array index on keep-alive:include/exclude --- src/core/components/keep-alive.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/components/keep-alive.js b/src/core/components/keep-alive.js index 5573feed7d1..0a5e4dea961 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -11,8 +11,10 @@ function getComponentName (opts: ?VNodeComponentOptions): ?string { return opts && (opts.Ctor.options.name || opts.tag) } -function matches (pattern: string | RegExp, name: string): boolean { - if (typeof pattern === 'string') { +function matches (pattern: string | RegExp | Array, name: string): boolean { + if (Array.isArray(pattern)) { + return pattern.indexOf(name) > -1 + } else if (typeof pattern === 'string') { return pattern.split(',').indexOf(name) > -1 } else if (isRegExp(pattern)) { return pattern.test(name) From b7ba9b28c186208b2b567d8b0ab4830017d64e63 Mon Sep 17 00:00:00 2001 From: jkzing Date: Fri, 23 Jun 2017 11:44:39 +0800 Subject: [PATCH 2/5] add Array in patternTypes --- 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 0a5e4dea961..4fc9d6f3b6d 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -5,7 +5,7 @@ import { getFirstComponentChild } from 'core/vdom/helpers/index' type VNodeCache = { [key: string]: ?VNode }; -const patternTypes: Array = [String, RegExp] +const patternTypes: Array = [String, RegExp, Array] function getComponentName (opts: ?VNodeComponentOptions): ?string { return opts && (opts.Ctor.options.name || opts.tag) From a201b25ea724e388c8e6ddfb24f7542707684f49 Mon Sep 17 00:00:00 2001 From: jkzing Date: Fri, 23 Jun 2017 11:57:50 +0800 Subject: [PATCH 3/5] fix flow type --- 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 4fc9d6f3b6d..070d8742cdf 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -11,7 +11,7 @@ function getComponentName (opts: ?VNodeComponentOptions): ?string { return opts && (opts.Ctor.options.name || opts.tag) } -function matches (pattern: string | RegExp | Array, name: string): boolean { +function matches (pattern: string | RegExp | Array, name: string): boolean { if (Array.isArray(pattern)) { return pattern.indexOf(name) > -1 } else if (typeof pattern === 'string') { From 8dd87085efaa488e883be4c011b9c2e54c601717 Mon Sep 17 00:00:00 2001 From: jkzing Date: Fri, 23 Jun 2017 22:42:41 +0800 Subject: [PATCH 4/5] add flow type for include/exclude in watch --- src/core/components/keep-alive.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/components/keep-alive.js b/src/core/components/keep-alive.js index 070d8742cdf..734570ca945 100644 --- a/src/core/components/keep-alive.js +++ b/src/core/components/keep-alive.js @@ -64,10 +64,10 @@ export default { }, watch: { - include (val: string | RegExp) { + include (val: string | RegExp | Array) { pruneCache(this.cache, this._vnode, name => matches(val, name)) }, - exclude (val: string | RegExp) { + exclude (val: string | RegExp | Array) { pruneCache(this.cache, this._vnode, name => !matches(val, name)) } }, From 762523d30263f369b3ab74a0ced2d82c56e1c4c8 Mon Sep 17 00:00:00 2001 From: jkzing Date: Fri, 23 Jun 2017 22:44:41 +0800 Subject: [PATCH 5/5] add test case --- .../component/component-keep-alive.spec.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/features/component/component-keep-alive.spec.js b/test/unit/features/component/component-keep-alive.spec.js index 7cc9f395772..1295c17f50e 100644 --- a/test/unit/features/component/component-keep-alive.spec.js +++ b/test/unit/features/component/component-keep-alive.spec.js @@ -272,6 +272,24 @@ describe('Component keep-alive', () => { sharedAssertions(vm, done) }) + it('include (array)', done => { + const vm = new Vue({ + template: ` +
+ + + +
+ `, + data: { + view: 'one', + ok: true + }, + components + }).$mount() + sharedAssertions(vm, done) + }) + it('exclude (string)', done => { const vm = new Vue({ template: ` @@ -308,6 +326,24 @@ describe('Component keep-alive', () => { sharedAssertions(vm, done) }) + it('exclude (array)', done => { + const vm = new Vue({ + template: ` +
+ + + +
+ `, + data: { + view: 'one', + ok: true + }, + components + }).$mount() + sharedAssertions(vm, done) + }) + it('include + exclude', done => { const vm = new Vue({ template: `