-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
141 lines (136 loc) · 3.3 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
<!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>组件动态切换与状态缓存</title>
<style>
.tab-button {
padding: 6px 10px;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
border: 1px solid #ccc;
cursor: pointer;
background: #f0f0f0;
margin-bottom: -1px;
margin-right: -1px;
}
.tab-button:hover {
background: #e0e0e0;
}
.tab-button.active {
background: #e0e0e0;
}
.tab {
border: 1px solid #ccc;
padding: 10px;
}
.posts-tab {
display: flex;
}
.posts-sidebar {
max-width: 40vw;
margin: 0;
padding: 0 10px 0 0;
list-style-type: none;
border-right: 1px solid #ccc;
}
.posts-sidebar li {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
cursor: pointer;
}
.posts-sidebar li:hover {
background: #eee;
}
.posts-sidebar li.selected {
background: #ccc;
}
.selected-post-container {
padding-left: 10px;
}
.selected-post > :first-child {
margin-top: 0;
padding-top: 0;
}
</style>
</head>
<body style="background: #f6f6d5">
<h2>组件动态切换与状态缓存 <sub><a style="text-decoration: none" href="../index.html">返回导航</a></sub></h2>
<div id="dynamic-component-demo">
<button
v-for="tab in tabs"
:key="tab"
:class="['tab-button', {'active': tab === currentTab}]"
@click="setCurrentTab(tab)"
>
{{tab}}
</button>
<keep-alive>
<component :is="currentTabComponent" class="tab"></component>
</keep-alive>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
Vue.component('tab-posts', {
data: function() {
return {
posts: [
{id: 1, title: 'post1', content: 'here is post1'},
{id: 2, title: 'post2', content: 'here is post2'},
{id: 3, title: 'post3', content: 'here is post3'}
],
currentPost: null
}
},
template: `
<div class="posts-tab">
<ul class="posts-sidebar">
<li
v-for="post in posts"
:key="post.id"
:class="{'selected': post === currentPost}"
@click="currentPost = post"
>
<b>{{post.title}}</b>
</li>
</ul>
<div class="selected-post-container">
<div v-if="currentPost" class="selected-post">
<h3>{{currentPost.title}}</h3>
<div v-html="currentPost.content"></div>
</div>
<b v-else>
Click a post in left sidebar!
</b>
</div>
</div>
`
})
Vue.component('tab-achieves', {
template: `<div>here is content of tab-achieves</div>`
})
const vm = new Vue({
el: '#dynamic-component-demo',
data: function() {
return {
tabs: ['posts', 'achieves'],
currentTab: 'posts'
}
},
computed: {
currentTabComponent: function() {
return 'tab-' + this.currentTab
}
},
methods: {
setCurrentTab: function(tab) {
this.currentTab = tab
}
}
})
</script>
</body>
</html>