-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTodoList.html
70 lines (64 loc) · 1.54 KB
/
TodoList.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TodoList</title>
<script src="./vue/vue.js"></script>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue">
<button @click="handleClick">提交</button>
</div>
<ul>
<!-- <li v-for="(item,index) of list" :key="index">{{item}}</li> -->
<todo-item
v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@delete="handleDelete"
>
</todo-item>
</ul>
</div>
<script>
/* 定义一个全局组件 */
// Vue.component('todo-item',{
// template: '<li>item</li>'
// })
/* 定义一个局部组件 要通过components注册之后才能显示 */
var TodoItem = {
props: ['content', 'index'], /* 要通过props接收之后才能显示 */
template: '<li @click="handleClick">{{content}}</li>',
methods: {
handleClick: function () {
this.$emit('delete', this.index)
}
}
}
new Vue({
// el:element 需要获取的元素,一定是html中的根容器元素
el: "#root",
components: {
'todo-item': TodoItem
},
// 数据的存储
data: {
inputValue: '',
list: []
},
methods: {
handleClick: function () {
this.list.push(this.inputValue)
this.inputValue = ''
},
handleDelete: function (index) {
this.list.splice(index,1)
}
}
})
</script>
</body>
</html>