Skip to content

Commit 51c595a

Browse files
jkzingyyx990803
authored andcommitted
feat(keep-alive): support Array for include and exclude (#5956)
* allow array index on keep-alive:include/exclude * add Array in patternTypes * fix flow type * add flow type for include/exclude in watch * add test case
1 parent e01c09a commit 51c595a

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

Diff for: src/core/components/keep-alive.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { getFirstComponentChild } from 'core/vdom/helpers/index'
55

66
type VNodeCache = { [key: string]: ?VNode };
77

8-
const patternTypes: Array<Function> = [String, RegExp]
8+
const patternTypes: Array<Function> = [String, RegExp, Array]
99

1010
function getComponentName (opts: ?VNodeComponentOptions): ?string {
1111
return opts && (opts.Ctor.options.name || opts.tag)
1212
}
1313

14-
function matches (pattern: string | RegExp, name: string): boolean {
15-
if (typeof pattern === 'string') {
14+
function matches (pattern: string | RegExp | Array<string>, name: string): boolean {
15+
if (Array.isArray(pattern)) {
16+
return pattern.indexOf(name) > -1
17+
} else if (typeof pattern === 'string') {
1618
return pattern.split(',').indexOf(name) > -1
1719
} else if (isRegExp(pattern)) {
1820
return pattern.test(name)
@@ -62,10 +64,10 @@ export default {
6264
},
6365

6466
watch: {
65-
include (val: string | RegExp) {
67+
include (val: string | RegExp | Array<string>) {
6668
pruneCache(this.cache, this._vnode, name => matches(val, name))
6769
},
68-
exclude (val: string | RegExp) {
70+
exclude (val: string | RegExp | Array<string>) {
6971
pruneCache(this.cache, this._vnode, name => !matches(val, name))
7072
}
7173
},

Diff for: test/unit/features/component/component-keep-alive.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ describe('Component keep-alive', () => {
272272
sharedAssertions(vm, done)
273273
})
274274

275+
it('include (array)', done => {
276+
const vm = new Vue({
277+
template: `
278+
<div v-if="ok">
279+
<keep-alive :include="['one']">
280+
<component :is="view"></component>
281+
</keep-alive>
282+
</div>
283+
`,
284+
data: {
285+
view: 'one',
286+
ok: true
287+
},
288+
components
289+
}).$mount()
290+
sharedAssertions(vm, done)
291+
})
292+
275293
it('exclude (string)', done => {
276294
const vm = new Vue({
277295
template: `
@@ -308,6 +326,24 @@ describe('Component keep-alive', () => {
308326
sharedAssertions(vm, done)
309327
})
310328

329+
it('exclude (array)', done => {
330+
const vm = new Vue({
331+
template: `
332+
<div v-if="ok">
333+
<keep-alive :exclude="['two']">
334+
<component :is="view"></component>
335+
</keep-alive>
336+
</div>
337+
`,
338+
data: {
339+
view: 'one',
340+
ok: true
341+
},
342+
components
343+
}).$mount()
344+
sharedAssertions(vm, done)
345+
})
346+
311347
it('include + exclude', done => {
312348
const vm = new Vue({
313349
template: `

0 commit comments

Comments
 (0)