-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (128 loc) · 3.38 KB
/
index.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
code {
display: block;
white-space: pre-wrap;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 0 1rem;
}
</style>
<script type="module">
import Vue from 'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.esm.browser.js'
Vue.component('LKeepAlive', {
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)
}
})
let counter = 0
Vue.component('Slow', {
name: 'Slow',
data: () => ({
counter: counter++,
value: 'loading...',
intervalCounter: 0
}),
created () {
setTimeout(() => {
this.value = 'done!!'
}, 1000)
setInterval(() => {
this.intervalCounter++
}, 1000)
},
template: `<div>{{ counter }} {{ _uid }} {{ value }} {{ intervalCounter }}</div>`
})
Vue.component('App', {
name: 'app',
data: () => ({
from: 0,
to: 10,
show: true,
singleExample: `
<button @click="show = !show">toggle</button>
<LKeepAlive>
<Slow v-if="show" />
</LKeepAlive>
`,
listExample: `
<input v-model="from" type="number" />
<input v-model="to" type="number" />
<LKeepAlive>
<Slow
v-for="n in items.slice(from, to)"
:key="n"
/>
</LKeepAlive>
`
}),
computed: {
items () {
return new Array(100)
.fill()
.map((_, i) => i)
.slice(this.from, this.to)
}
},
template: `
<div>
This is a single item with LKeepAlive<br>
<code v-text="singleExample" />
<button @click="show = !show">toggle</button>
<LKeepAlive>
<Slow v-if="show" />
</LKeepAlive>
<hr style="margin: 5rem 0"/>
This is a list with LKeepAlive<br>
<code v-text="listExample" />
<input v-model="from" type="number" />
<input v-model="to" type="number" />
<LKeepAlive>
<Slow
v-for="n in items"
:key="n"
/>
</LKeepAlive>
</div>
`
})
Vue.config.productionTip = false
Vue.config.devtools = true
new Vue({
render: function (h) { return h('App') }
}).$mount('#app')
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>