-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive-component.html
More file actions
77 lines (75 loc) · 2.28 KB
/
active-component.html
File metadata and controls
77 lines (75 loc) · 2.28 KB
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
<!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 type="text/css">
.posts-sidebar li:hover{
background: #eee;
}
.posts-sidebar li.selected {
background: lightblue;
}
</style>
</head>
<body>
<div id="app">
<keep-alive>
<component :is="currentView"></component>
</keep-alive>
<button @click="handleChangeView('A')">切换到A</button>
<button @click="handleChangeView('B')">切换到B</button>
<button @click="handleChangeView('C')">切换到C</button>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
var app = new Vue({
el: "#app",
components: {
comA: {
data: function () {
return {
posts: [
{ id: 1, title: "Cat Ipsum", content: 'Dont you ' },
{ id: 2, title: "Cat ", content: 'Dont you ' },
{ id: 3, title: "Cat Ipsum", content: 'Dont you ' }
],
selectPost: null
}
},
template: `
<div>
<ul class="posts-sidebar">
<li
v-for="post in posts"
:key="post.id"
:class="{ selected: post === selectPost }"
@click="selectPost = post"
>
{{ post.title }}
</li>
</ul>
</div>
`
},
comB: {
template: '<div>组件B</div>'
},
comC: {
template: '<div>组件C</div>'
}
},
data: {
currentView: 'comA'
},
methods: {
handleChangeView: function (component) {
this.currentView = 'com' + component;
}
}
})
</script>
</html>