-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (28 loc) · 860 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default {
name: 'LKeepAlive',
data: () => ({
cache: new Map()
}),
render (h) {
if (!this.$scopedSlots.default) return
const vnodes = this.$scopedSlots.default()
const children = []
for (const [index, vnode] of vnodes.entries()) {
const componentOptions = vnode && vnode.componentOptions
if (!componentOptions) return vnode
const key = vnode.key == null
? index + componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key
if (this.cache.has(key)) {
vnode.componentInstance = this.cache.get(key).componentInstance
} else {
this.cache.set(key, vnode)
}
vnode.data.keepAlive = true
children.push(vnode)
}
return children.length === 1
? children[0]
: h('div', null, children)
}
}