vue3-todoMVC练习
前端小菜-贺俊兰 2020-08-25 VUE
# todoMVC 简介
参照todeMVC网站,复制样式,做个类似的例子。打开NewWork,找到todomvc-app-css的index.css,放到本地就可以了。
# CODE
啥也别说了,上才艺
<template>
<section class="todoapp">
<header class="header">
<h1>Vue3 todos</h1>
<input class="new-todo" placeholder="想干的事" v-model="newTodo" @keyup.enter="addTodo" />
</header>
<section class="main">
<input id="toggle-all" type="checkbox" class="toggle-all" />
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<li v-for="todo in todos" class="todo" :key="todo.id">
<div class="view">
<input type="checkbox" :value="todo.isCheck" class="toggle" />
<label>{{todo.title}}</label>
<button class="destroy" @click="removeTodo(todo)"></button>
</div>
</li>
</ul>
</section>
</section>
</template>
<script>
import { reactive, toRefs } from "vue";
export default {
setup() {
const state = reactive({
newTodo: "",
todos: [{ id: 1, title: "练习", isCheck: false }],
});
function addTodo() {
const value = state.newTodo && state.newTodo.trim();
if (!value) return;
state.todos.push({
id: state.todos.length + 1,
title: value,
isCheck: false,
});
state.newTodo = "";
}
function removeTodo(item) {
state.todos = state.todos.filter((empty) => empty.id != item.id);
}
return {
...toRefs(state),
addTodo,
removeTodo,
};
},
};
</script>
<style scoped>
.toggle {
left: 0;
}
</style>
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
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