Skip to content

Commit 27009bc

Browse files
committed
include/exclude for keep-alive
1 parent 03043b9 commit 27009bc

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/core/components/keep-alive.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
1+
/* @flow */
2+
13
import { callHook } from 'core/instance/lifecycle'
24
import { getFirstComponentChild } from 'core/vdom/helpers/index'
35

6+
const patternTypes = [String, RegExp]
7+
8+
function matches (pattern: string | RegExp, name: string): boolean {
9+
if (typeof pattern === 'string') {
10+
return pattern.split(',').indexOf(name) > -1
11+
} else {
12+
return pattern.test(name)
13+
}
14+
}
15+
416
export default {
517
name: 'keep-alive',
618
abstract: true,
19+
props: {
20+
include: patternTypes,
21+
exclude: patternTypes
22+
},
723
created () {
824
this.cache = Object.create(null)
925
},
1026
render () {
11-
const vnode = getFirstComponentChild(this.$slots.default)
27+
const vnode: VNode = getFirstComponentChild(this.$slots.default)
1228
if (vnode && vnode.componentOptions) {
13-
const opts = vnode.componentOptions
29+
const opts: VNodeComponentOptions = vnode.componentOptions
30+
// check pattern
31+
const name = opts.tag || opts.Ctor.options.name
32+
if (name && (
33+
(this.include && !matches(this.include, name)) ||
34+
(this.exclude && matches(this.exclude, name))
35+
)) {
36+
return vnode
37+
}
1438
const key = vnode.key == null
1539
// same constructor may get registered as different local components
1640
// so cid alone is not enough (#3269)
17-
? opts.Ctor.cid + '::' + opts.tag
41+
? opts.Ctor.cid + (opts.tag ? `::${opts.tag}` : '')
1842
: vnode.key
1943
if (this.cache[key]) {
2044
vnode.child = this.cache[key].child

0 commit comments

Comments
 (0)