From 7cdb221faf1c85d1895ca8bc1b440eb8b8155605 Mon Sep 17 00:00:00 2001 From: roger Date: Fri, 20 Jan 2023 11:39:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90todoList=E7=9A=84=E5=AD=A6?= =?UTF-8?q?=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20_脚手架/vue_code/src/App.vue | 22 ++++- .../vue_code/src/components/TodoBottom.vue | 84 +++++++++++++------ .../vue_code/src/components/TodoHeader.vue | 3 + .../vue_code/src/components/TodoItem.vue | 19 +++-- .../vue_code/src/components/TodoList.vue | 25 +++--- 5 files changed, 109 insertions(+), 44 deletions(-) diff --git a/20_脚手架/vue_code/src/App.vue b/20_脚手架/vue_code/src/App.vue index 5371cf3..335b63b 100644 --- a/20_脚手架/vue_code/src/App.vue +++ b/20_脚手架/vue_code/src/App.vue @@ -3,8 +3,8 @@
- - + +
@@ -25,6 +25,7 @@ export default { }, data() { return { + // 预置测试数据 todos: [ {id: "001", title: "产品", done: true}, {id: "002", title: "开发", done: true}, @@ -32,19 +33,36 @@ export default { ] } }, + // 由于数据需要被不同的子组件访问,暂时没有学习数据的处理方法,所以将数据放置在父组件App中 + // 导致处理数据的相关方法也要放到App中,通过组件间参数传递的方式传到各个组件 methods: { + // 添加todo任务,绑定到TodoHeader中 addTodo(todoObj) { this.todos.unshift(todoObj) }, + // 勾选和取消勾选todo任务,穿透TodoList绑定到TodoItem中 checkTodo(id) { this.todos.forEach((todo) => { if (todo.id === id) todo.done = !todo.done }) }, + // 删除单个todo任务,穿透TodoList绑定到TodoItem中 deleteTodo(id) { + this.todos = this.todos.filter(todo => todo.id !== id) + }, + + // 勾选或取消勾选全部todo任务,绑定到TodoBottom中 + checkAllTodo(done) { + this.todos.forEach((todo) => todo.done = done) + }, + // 清除全部已完成的todo任务,绑定到TodoBottom中 + clearAllTodo() { + this.todos = this.todos.filter((todo) => { + return !todo.done + }) } } } diff --git a/20_脚手架/vue_code/src/components/TodoBottom.vue b/20_脚手架/vue_code/src/components/TodoBottom.vue index 5e56a19..4ac67d0 100644 --- a/20_脚手架/vue_code/src/components/TodoBottom.vue +++ b/20_脚手架/vue_code/src/components/TodoBottom.vue @@ -1,42 +1,74 @@ \ No newline at end of file diff --git a/20_脚手架/vue_code/src/components/TodoHeader.vue b/20_脚手架/vue_code/src/components/TodoHeader.vue index c967cd1..b0c7dca 100644 --- a/20_脚手架/vue_code/src/components/TodoHeader.vue +++ b/20_脚手架/vue_code/src/components/TodoHeader.vue @@ -11,11 +11,14 @@ export default { name: "TodoHeader", props: ['addTodo'], data() { + // 初始化title已显示placeholder内容 return { title: '' } }, methods: { + // 添加todo任务,trim用户输入数据后判断是否为空,如果内容正常则生成任务的对象数据 + // 将任务的对象数据回写到App中的todo中 add() { if (!this.title.trim()) return alert("输入不能为空") const todoObj = {id: nanoid(), title: this.title, done: false} diff --git a/20_脚手架/vue_code/src/components/TodoItem.vue b/20_脚手架/vue_code/src/components/TodoItem.vue index f3f66e0..b565bdc 100644 --- a/20_脚手架/vue_code/src/components/TodoItem.vue +++ b/20_脚手架/vue_code/src/components/TodoItem.vue @@ -2,19 +2,27 @@
  • - +
  • @@ -27,6 +29,7 @@ export default { border-radius: 2px; padding: 0px; } + .todo-empty { height: 40px; line-height: 40px;