vue keepalived

vue 使用keepalived 避免组件被销毁,在内存中可保持数据

源码

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
<html>
<header>
<title>Vue.js 组件</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</header>

<body>
<div id="box">
<button @click="whoami='page1'">1</button>
<button @click="whoami='page2'">2</button>
<button @click="whoami='page3'">3</button>

<!-- keep-alive 保持组件,避免重复渲染 -->
<keep-alive>
<!-- component is属性 动态渲染组件 -->
<component :is="whoami"></component>
</keep-alive>
</div>
</body>

<script>
var vm = new Vue({
el: "#box",
data: {
whoami: 'page1',
},
components: {
"page1": { template: `<div> page1 </br> <input type="text"> <div>` },
"page2": { template: `<div> page2 </br> <input type="text"> <div>` },
"page3": { template: `<div> page3 </br> <input type="text"> <div>` }

}
})
</script>

</html>