使用vue2.0实现一个TODO应用

朋友,然后爱人,最后什么都不是。
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="js/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" id="app">
<div class="row">
<div class="col-md-8 col-md-offset-2" style="margin-top: 90px;">
<div class="panel panel-default">
<div class="panel-heading">vue demo</div>
<div class="panel-body">
<h1>事件个数{{ todoCount }}</h1>
<todo-items :todos="todos"></todo-items>
<todo-form :todos="todos"></todo-form>
</div>
</div>
</div>
</div>
</div>
</body>

<script type="text/x-template" id="todo-items">
<ul class="list-group">
<li class="list-group-item" v-bind:class="{ 'text-danger':todo.status }" v-for="(todo, index) in todos">
{{ todo.title}}
<button class="btn btn-danger btn-xs pull-right" v-on:click="deleteTodo(index)">删除</button>
<button class="btn btn-xs pull-right" v-bind:class="[todo.status ? 'btn-danger' : 'btn-success' ]" v-on:click="complete(todo)">
{{ todo.status ? '撤销' : '完成' }}
</button>
</li>
</ul>
</script>
<script type="text/x-template" id="todo-forms">
<form v-on:submit.prevent="addTodo(newTodo)">
<div class="form-group">
<label for="add-todo">事件名称</label>
<input type="text" class="form-control" id="add-todo" v-model="newTodo.title" >
</div>
<button type="submit" class="btn btn-default">添加事件</button>
</form>
</script>
<script src="js/vue.js"></script>
<script>
Vue.component('todo-form', {
template:'#todo-forms',
props: ['todos'],
data() {
return {
newTodo: {'title': '', 'status': false}
};
},
methods: {
addTodo(newTodo) {
this.todos.push(newTodo);
this.newTodo = {'title': '', 'status': false};
}
}
});

Vue.component('todo-items', {
template:'#todo-items',
props: ['todos'],
methods: {
deleteTodo(index) {
this.todos.splice(index, 1);
},
complete(todo) {
todo.status = !todo.status;
}
}
});


new Vue({
el:'#app',
data: {
message: 'yangzie',
todos: [
{'title': '吃早餐', 'status': false},
{'title': '洗漱', 'status': false}
]
},
computed: {
todoCount() {
return this.todos.length;
}
}
});
</script>
</html>