todolist学习临时代码2-编写部分逻辑但是遇到问题

Error in v-on handler: "TypeError: this.checkTodo is not a function"
new_branch1
roger 2 years ago
parent 7651eb4e6b
commit 435cefc3df
  1. 29
      20_脚手架/vue_code/src/App.vue
  2. 21
      20_脚手架/vue_code/src/components/TodoHeader.vue
  3. 16
      20_脚手架/vue_code/src/components/TodoItem.vue
  4. 13
      20_脚手架/vue_code/src/components/TodoList.vue

@ -2,8 +2,8 @@
<div id="root"> <div id="root">
<div class="todo-container"> <div class="todo-container">
<div class="todo-wrap"> <div class="todo-wrap">
<TodoHeader/> <TodoHeader :addTodo="addTodo"/>
<TodoList/> <TodoList :todos="todos"/>
<TodoBottom/> <TodoBottom/>
</div> </div>
</div> </div>
@ -22,6 +22,30 @@ export default {
TodoHeader, TodoHeader,
TodoList, TodoList,
TodoBottom TodoBottom
},
data() {
return {
todos: [
{id: "001", title: "产品", done: true},
{id: "002", title: "开发", done: true},
{id: "003", title: "测试", done: false}
]
}
},
methods: {
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
deleteTodo(id) {
}
} }
} }
</script> </script>
@ -63,6 +87,7 @@ body {
width: 600px; width: 600px;
margin: 0 auto; margin: 0 auto;
} }
.todo-container .todo-wrap { .todo-container .todo-wrap {
padding: 10px; padding: 10px;
border: 1px solid #ddd; border: 1px solid #ddd;

@ -1,12 +1,29 @@
<template> <template>
<div class="todo-header"> <div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车确认"> <input type="text" placeholder="请输入你的任务名称,按回车确认" v-model="title" @keyup.en.enter="add">
</div> </div>
</template> </template>
<script> <script>
import {nanoid} from "nanoid"
export default { export default {
name: "TodoHeader" name: "TodoHeader",
props: ['addTodo'],
data() {
return {
title: ''
}
},
methods: {
add() {
if (!this.title.trim()) return alert("输入不能为空")
const todoObj = {id: nanoid(), title: this.title, done: false}
this.addTodo(todoObj)
this.title = ''
}
}
} }
</script> </script>

@ -1,8 +1,8 @@
<template> <template>
<li> <li>
<label> <label>
<input type="checkbox" class="TodoItem"> <input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">
<span>xxxxxxx</span> <span>{{todo.title}}</span>
</label> </label>
<button class="btn btn-danger">删除</button> <button class="btn btn-danger">删除</button>
</li> </li>
@ -11,15 +11,13 @@
<script> <script>
export default { export default {
name: "TodoItem", name: "TodoItem",
data() { props: ['todo', 'checkTodo'],
return { methods: {
todoList: [ handleCheck(id) {
{id: "001", title: "产品", done: true}, this.checkTodo(id)
{id: "002", title: "开发", done: true},
{id: "003", title: "测试", done: false}
]
} }
} }
} }
</script> </script>

@ -1,10 +1,12 @@
<template> <template>
<div> <div>
<ul class="todo-list"> <ul class="todo-list">
<TodoItem/> <TodoItem
<TodoItem/> v-for="todoObj in todos"
<TodoItem/> :key="todoObj.id"
<TodoItem/> :todo="todoObj"
:checkTodo="checkTodo"
/>
</ul> </ul>
</div> </div>
</template> </template>
@ -13,7 +15,8 @@
import TodoItem from "@/components/TodoItem.vue"; import TodoItem from "@/components/TodoItem.vue";
export default { export default {
name: "TodoList", name: "TodoList",
components: {TodoItem} components: {TodoItem},
props: ['todos', 'checkTodo']
} }
</script> </script>

Loading…
Cancel
Save