计算所有事项的总和
就是说要计算app中的todos的长度,那么就需要把todos传给footer
这样的话,在footer已经有todos了,我们就可以直接得到它的长度
这样就可以计算所有事项的总和了
计算已完成事项的总和
方法一
因为这个计算比较复杂些,所以通过computed来写
这样就能计算出已完成事项的数量
也可以用reduce,它可以进行条件统计
如果对reduce不熟悉可以点击此链接
Array.prototype.reduce() - JavaScript | MDN (mozilla.org)
了解一下其中的用法
那么就可以计算出已完成的事项
这结的主要代码如下:
UserFooter.vue
<template>
<div class="todo-footer">
<label>
<input type="checkbox" />
</label>
<span> <span>已完成{{ doneTodal }}</span> / 全部{{ todos.length }} </span>
<button class="btn btn-danger">清除已完成任务</button>
</div>
</template>
<script>
// 组件交互相关的代码(数据、方法等等)
export default {
name: "UserFooter",
data() {
return {};
},
props: ["todos"],
computed: {
doneTodal() {
// let i = 0;
// this.todos.forEach((todo) => {
// if (todo.done) i++;
// });
// return i;
// return this.todos.reduce((pre, current) => {
// console.log("@", pre, current);
// return pre + (current.done ? 1 : 0);
// }, 0);
return this.todos.reduce((pre, current) => pre + (current.done ? 1 : 0),0);
// const a = this.todos.filter(todo => todo.done == true);
// return a.length;
},
},
};
</script>
<style scoped>
/* footer */
.todo-footer {
height: 40px;
line-height: 40px;
padding: 0 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
vertical-align: middle;
margin-right: 5px;
position: relative;
top: -1px;
cursor: pointer;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>
App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<UserHeader :AddTodoObj="AddTodoObj" />
<UserList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo" />
<UserFooter :todos="todos" />
</div>
</div>
</div>
</template>
<script>
// 引入组件
import UserHeader from "./components/UserHeader.vue";
import UserList from "./components/UserList.vue";
import UserFooter from "./components/UserFooter.vue";
export default {
name: "App",
components: { UserHeader, UserList, UserFooter },
data() {
return {
todos: [
{ id: "001", title: "打代码", done: true },
{ id: "002", title: "看电视剧", done: true },
{ id: "003", title: "洗碗", done: false },
],
};
},
methods: {
// 添加一个todo
AddTodoObj(todoObj) {
this.todos.unshift(todoObj);
// console.log("我是App组件,我收到了数据", x);
},
// 勾选or取消勾选一个todo
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done;
});
},
// 删除一个todo
deleteTodo(id) {
this.todos = this.todos.filter((todo) => todo.id !== id);
},
},
};
</script>
<style>
/* base */
body {
background-color: #fff;
/* margin: 0; */
}
.todo-container {
width: 600px;
margin: 0 auto;
/* background-color: antiquewhite; */
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
.btn {
display: inline-block;
padding: 4px 12px;
/* margin-bottom: 0; */
font-size: 14px;
/* line-height: 20px; */
/* text-align: center; */
/* vertical-align 用来指定行内元素(inline)或表格单元格(table-cell)元素的垂直对齐 方式 */
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0px rgba(255, 255, 255, 0.2),
0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn:focus {
/* outline: none; */
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
</style>