要求:
本节把涉及到 子给父 传消息的地方,都修改为 自定义事件。
因为本节我们只关心 子和父 之间的事情,所以本节只修改 MyHeader MyFooter 和 App 之间的内容, 暂时不修改MyList和MyItem(因为这些涉及 爷孙),如下图所示:

调试
使用 Vue开发者工具,还有一个选项卡可以查看事件,如下图所示:

源码
App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<!-- 父组件App.vue 给 子组件MyHeader 传递了一个函数 addTodo() -->
<!-- 把下面的方法 addTodo() 给子组件 MyHeader
<MyHeader> 自定义组件标签名
:(v-bind 简写) 动态绑定指令
addTodo 传递给 子组件MyHeader 的属性名(子组件通过 props接收)
"addTodo" 父组件App.vue 中 methods 里的方法名
<MyHeader :addTodo="addTodo"/>
-->
<!-- 083 将上面一句 调整为 自定义事件,直接修改为 @符号即可-->
<MyHeader @addTodo="addTodo"/>
<!-- 父组件App.vue 给 子组件MyList 传递了一个属性 todos -->
<!-- 将父组件的 todos 数据传递给子组件
<MyList> 自定义组件标签名
:(v-bind 简写) 动态绑定指令
todos 传递给子组件的属性名(子组件通过 props接收)
"todos" 父组件 data 中的属性名(数据来源)
-->
<!-- App.vue 如果想给 孙子组件 MyItem.vue 传递数据(是否勾选),必须先给 MyList子组件 -->
<MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
<!-- 注意: 此处的 :todos 属性传递过去的 "todos"是数组 ∈ 数据
:checkAllTodo 属性传递过去的 "checkAllTodo" 是函数 ,想让儿子把数据传递过来
:clearAllTodo 属性传递过去的 "checkAllTodo" 是函数 ,想让儿子把数据传递过来
<MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/>
-->
<MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div>
</template>
<script>
import MyHeader from './components/MyHeader.vue'
import MyFooter from './components/MyFooter.vue'
import MyList from './components/MyList.vue'
export default {
name:'App',
components:{
MyHeader,
MyList,
MyFooter
},
//从MyList.vue里,将 data() 移到这里。
data(){
return {
/*todos :[
{id:'0001',title:'抽烟', done:true},
{id:'0002',title:'喝酒', done:false},
{id:'0003',title:'开车', done:true}
]*/
//079
//todos: JSON.parse(localStorage.getItem('todos'))
//如果 localStorage.getItem('todos') 读取无数据返回null时,无法JSON.parse() ,所以增加 || []
//意思是: 如果 localStorage.getItem('todos') 为 true ,我就用。 如果用不了,我就用右侧的空数组[]
todos: JSON.parse(localStorage.getItem('todos')) || []
}
},
//如果想实现 子组件 MyHeader.vue 传递数据 给 父组件App.vue,那么就在 此处父组件位置,准备好一个方法。
methods: {
//添加一个todo
addTodo(todoObj){
console.log('我是 App组件, 我收到了数据:', todoObj)
//父组件App.vue 在接收到 子组件MyHeader.vue 传递过来的参数x 之后,再传递给 子组件MyList.vue
this.todos.unshift(todoObj)
},
//勾选 or 取消勾选 一个todo
checkTodo(id){
//将 todos数组 进行遍历:
this.todos.forEach((todo) => { //拿到其中的 todo项
//如果当前项的todo.id 和 传过来的 id 相同,那么就把当前项的done取反。
if(todo.id== id) {
todo.done=!todo.done
}
})
},
//删除一个 todo
deleteTodo(id){
this.todos = this.todos.filter((todo) => {
return todo.id !== id
})
},
//全选 or 取消全选
checkAllTodo(done){
this.todos.forEach((todo)=>{
todo.done = done
})
},
//076 清除所有已完成的todo
clearAllTodo(){
this.todos = this.todos.filter((todo) => {
return !todo.done //如果当前 todo项,已完成就是true,那么返回就是 !true = false,返回 false 就是过滤掉当前 todo项 啦。
})
}
},
//079 添加监视 todos,只要监测到 todos 发生变化,那么 todos 就一定是最新的。
watch:{
//实际是 newvalue
//由于默认监视的是第0层,所以如果修改了其中 todos.done 深层,就无法监测到。
/*todos(value){
localStorage.setItem('todos', JSON.stringify(value))
}*/
//完整版写法:
todos:{
deep:true, //开启深度监视 这样就可以监测到 todos对象中的每个属性变化
handler(value){
localStorage.setItem('todos', JSON.stringify(value))
}
}
}
}
</script>
<style>
/*base*/
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
/*main*/
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>components\MyHeader.vue
<template>
<div class="todo-header">
<!-- 添加一个键盘事件
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/>
-->
<!-- 添加一个双向绑定的属性 title-->
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add" />
</div>
</template>
<script>
//引入 nanoid ,是 uuid 的一个mini版本。 如果提示错误,需要安装 npm i nanoid
import {nanoid} from 'nanoid'
export default {
name:'MyHeader',
data() {
return {
title:''
}
},
//因为父组件 App.vue 传递过来一个函数 addTodo, 所以我这里需要接收一下:
//083 屏蔽
//props:['addTodo'], //那么此时一接收, addTodo 方法就出现在了 MyHeader 身上了。
methods: {
add(e){
//console.log(this.title)
//console.log(e.target.value)
//
if(!this.title.trim()) return alert('输入不能为空')
//将 用户的输入 包装成一个 todo对象
const todoObj = {id:nanoid(), title:e.target.value, done:false}
console.log(todoObj)
//上面通过 props 接收到了 父组件传递过来的 receive()函数, 所以就可以在此处把 todoObj 通过 receive() 传过去:
//this.addTodo(todoObj);
//083 触发 App.vue 绑定当前控件时,绑定的自定义事件
this.$emit('addTodo', todoObj)
//把输入框清空
//e.target.value = ''
//通过 title 清空
this.title = ''
//
}
},
}
</script>
<style scoped>
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>components\MyFooter.vue
<template>
<!-- 此处 total>0 就会为 true, 显示; total=0就是 false,就会为false, 不显示 -->
<div class="todo-footer" v-show="total">
<label>
<!--
//076
<input type="checkbox" :checked="doneTotal===total"/>
//模板里不想写这么复杂:
<input type="checkbox" :checked="isAll" @change="checkAll"/>
//还可以继续优化为:
-->
<input type="checkbox" v-model="isAll"/>
</label>
<span>
<!--
<span>已完成{{ doneTotal }}</span> / 全部 {{ todos.length }}
//076 上面的 todos.length 可以继续优化到 计算属性中.
-->
<span>已完成{{ doneTotal }}</span> / 全部 {{ total }}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name:'MyFooter',
//接收:
props:['todos', 'checkAllTodo', 'clearAllTodo'],
computed:{
//076 把 todos.length 放到这里.
total(){
//注意:在这里需要加 this. 找到 todos。 模板里不用加 this.
return this.todos.length
},
//075
doneTotal(){
//return 99
//.reduce() 数组身上的方法,用于条件统计。
/*const x = this.todos.reduce((pre, current)=>{ //第0个参数: 函数 其中参数pre是上一次调用 (pre, current)=>{} 的返回值。 如果没有返回值,那么以后调用(pre, current)=>{}的pre就是 undefined 。
console.log('@', pre, current) //第1个参数: current 就是每1个todo项
return pre+(current.done ? 1:0)
},0) //第1个参数: 统计的初值为0
console.log('###', x)
return x*/
//精简写法:
return this.todos.reduce((pre,current) => pre+(current.done?1:0), 0)
},
//076
isAll: {
get(){
//注意: 下面等号左边 拿的是上面的计算属性 doneTotal()
// 等号右边 拿的是上面的计算属性 total()
//说明:计算属性是可以套娃的。
return this.doneTotal == this.total && this.total > 0
},
set(value){
console.log('@@@', value) //true/false
//this.checkAllTodo(value)
//083
this.$emit('checkAllTodo', value)
}
}
},
//076
methods:{
checkAll(e){
console.log(e.target.checked) //这里是 借助 DOM 拿到它的属性,并没有通过DOM操作(写)它。
this.checkAllTodo(e.target.checked)
},
clearAll(){
//this.clearAllTodo()
//083
this.$emit('clearAllTodo')
}
}
}
</script>
<style scoped>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>
