1
1
<template >
2
- <div class =" app" >
3
- <input type =" text" v-model =" newTodo" @keyup.enter =" addTodo" />
4
- <div >
5
- <input type =" checkbox" v-model =" allDone" /> Select All
6
- <ul >
7
- <li
8
- v-for =" todo in todos"
9
- :key =" todo.id"
10
- :class =" {completed: todo.completed, editing: todo == editedTodo}"
11
- >
12
- <div class =" view" >
13
- <input type =" checkbox" v-model =" todo.completed" />
14
- <label @dblclick =" editTodo(todo)" >{{todo.title}}</label >
15
- <button @click =" removeTodo(todo)" >X</button >
16
- </div >
17
- <input
18
- class =" edit"
19
- type =" text"
20
- v-model =" todo.title"
21
- v-todo-focus =" todo == editedTodo"
22
- @blur =" doneEdit(todo)"
23
- @keyup.enter =" doneEdit(todo)"
24
- @keyup.esc =" cancelEdit(todo)"
25
- />
26
- </li >
27
- </ul >
28
- <div >
29
- <span >
30
- <strong v-text =" remaining" ></strong >
31
- {{remaining}} left
2
+ <section class =" todoapp" >
3
+ <header class =" header" >
4
+ <h1 >Vue2 todos</h1 >
5
+ <input class =" new-todo"
6
+ placeholder =" What needs to be done?"
7
+ v-model =" newTodo"
8
+ @keyup.enter =" addTodo" >
9
+ </header >
10
+ <section class =" main" v-show =" todos.length" v-cloak >
11
+ <input class =" toggle-all" type =" checkbox" v-model =" allDone" >
12
+ <ul class =" todo-list" >
13
+ <li v-for =" todo in todos"
14
+ class =" todo"
15
+ :key =" todo.id"
16
+ :class =" { completed: todo.completed, editing: todo == editedTodo }" >
17
+ <div class =" view" >
18
+ <input class =" toggle" type =" checkbox" v-model =" todo.completed" >
19
+ <label @dblclick =" editTodo(todo)" >{{ todo.title }}</label >
20
+ <button class =" destroy" @click =" removeTodo(todo)" ></button >
21
+ </div >
22
+ <input class =" edit" type =" text"
23
+ v-model =" todo.title"
24
+ v-todo-focus =" todo == editedTodo"
25
+ @blur =" doneEdit(todo)"
26
+ @keyup.enter =" doneEdit(todo)"
27
+ @keyup.esc =" cancelEdit(todo)" >
28
+ </li >
29
+ </ul >
30
+ </section >
31
+ <footer class =" footer" v-show =" todos.length" v-cloak >
32
+ <span class =" todo-count" >
33
+ <strong >{{ remaining }}</strong > left
32
34
</span >
33
- <button @click =" removeCompleted" v-show =" todos.length > remaining" >Clear completed</button >
34
- </div >
35
- </div >
36
- </div >
35
+ <button class =" clear-completed" @click =" removeCompleted" v-show =" todos.length > remaining" >
36
+ Clear completed
37
+ </button >
38
+ </footer >
39
+ </section >
40
+
37
41
</template >
38
42
39
43
<script >
44
+
45
+ import storage from ' ./local'
46
+
40
47
export default {
41
48
data () {
42
49
return {
43
- todos: [
44
- {
45
- id: " 1" ,
46
- title: " 吃饭" ,
47
- completed: false
48
- },
49
- {
50
- id: " 2" ,
51
- title: " 睡觉" ,
52
- completed: false
53
- }
54
- ],
50
+ todos: storage .get (),
51
+ // todos: [
52
+ // {
53
+ // id: "1",
54
+ // title: "吃饭",
55
+ // completed: false
56
+ // },
57
+ // {
58
+ // id: "2",
59
+ // title: "睡觉",
60
+ // completed: false
61
+ // }
62
+ // ],
55
63
newTodo: " " ,
56
64
editedTodo: null
57
65
};
58
66
},
59
67
60
- // computed properties
61
- // http://vuejs.org/guide/computed.html
62
68
computed: {
63
69
remaining : function () {
64
70
return this .todos .filter (todo => ! todo .completed ).length ;
@@ -89,16 +95,21 @@ export default {
89
95
completed: false
90
96
});
91
97
this .newTodo = " " ;
98
+ storage .set (this .todos )
92
99
},
93
100
94
101
removeTodo : function (todo ) {
95
102
var index = this .todos .indexOf (todo);
96
103
this .todos .splice (index, 1 );
104
+ storage .set (this .todos )
105
+
97
106
},
98
107
99
108
editTodo : function (todo ) {
100
109
this .beforeEditCache = todo .title ;
101
110
this .editedTodo = todo;
111
+ storage .set (this .todos )
112
+
102
113
},
103
114
104
115
doneEdit : function (todo ) {
@@ -110,6 +121,8 @@ export default {
110
121
if (! todo .title ) {
111
122
this .removeTodo (todo);
112
123
}
124
+ storage .set (this .todos )
125
+
113
126
},
114
127
115
128
cancelEdit : function (todo ) {
@@ -119,6 +132,8 @@ export default {
119
132
120
133
removeCompleted : function () {
121
134
this .todos = this .todos .filter (todo => todo .completed );
135
+ storage .set (this .todos )
136
+
122
137
}
123
138
},
124
139
0 commit comments