-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_todo.html
83 lines (80 loc) · 1.75 KB
/
3_todo.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
<!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>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<todo></todo>
</div>
<template id="tplTodoList">
<ul>
<todo-item v-for="(task,i) in tasks" :i="i" :task="task" @del="del(i)"></todo-item>
</ul>
</template>
<template id="tplTodoItem">
<li>{{i+1}} - {{task}} <button @click="del(i)">x</button></li>
</template>
<template id="tplTodo">
<div>
<h1>待办事项列表</h1><hr>
<todo-add @add="add"></todo-add>
<todo-list :tasks="tasks"></todo-list>
</div>
</template>
<template id="tplTodoAdd">
<div>
<input v-model="task"/><button @click="add()">添加</button>
</div>
</template>
<script>
var todoAdd={//<todo-add>
template:"#tplTodoAdd",
data(){ return { task:"" } },
methods:{
add(){
this.$emit("add",this.task);
this.task="";
}
}
};
var todoItem={//<todo-item>
template:"#tplTodoItem",
props:["i","task"],
methods:{
del(i){
this.$emit("del",i);//爹的del(i)
}
}
};
var todoList={//<todo-list>
template:"#tplTodoList",
props:["tasks"],//准备接父组件给的tasks
methods:{
del(i){ this.tasks.splice(i,1); }
},
components:{ todoItem }//<todo-item>
};
Vue.component("todo",{
template:"#tplTodo",
data(){
return { tasks:["吃饭","睡觉","打亮亮"] }
},
methods:{
add(task){
this.tasks.push(task);
}
},
components:{ todoAdd, todoList }
//<todo-add> <todo-list>
});
</script>
<script>
new Vue({ el:"#app" })
</script>
</body>
</html>