1、MyHeader.vue
<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"></div>
</template><script>import {nanoid} from 'nanoid'export default {name: "MyHeader",props:['addTodo'],data(){return{title:''}},methods:{add(){if(!this.title.trim()) alert("输入不能为零") ;const obj = {id:nanoid(),title: this.title,done:false};this.addTodo(obj);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>
2、MyList.vue
<template><ul class="todo-main"><my-item v-for="todobj in todos" :key="todobj.id" :todo="todobj" :changeDone="changeDone" :deleteToto="deleteToto"></my-item></ul>
</template>
<script>import MyItem from './MyItem'export default {name: "MyList",components:{MyItem},props:['todos','changeDone','deleteToto'],computed:{}}
</script><style scoped>/*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;}
</style>
3、MyItem.vue
<template><li><label><input type="checkbox" :checked="todo.done" @change="changecheckItem(todo.id)"/><span>{{todo.title}}</span></label><button class="btn btn-danger" @click="removeTodo(todo.id)">删除</button></li>
</template>
<script>export default {name: "MyItem",props:['todo','changeDone','deleteToto'],data(){return{}},methods:{changecheckItem(id){this.changeDone(id);},removeTodo(id){if(confirm('是否删除')){this.deleteToto(id);}}}}
</script><style scoped>/*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;}li:hover button{display: block;}
</style>
4、MyFoot.vue
<template><div class="todo-footer" v-show="total" ><label><input type="checkbox" v-model="isAll" ></label><span><span>已完成{{totalDone}}</span> / 全部{{todos.length}}</span><button class="btn btn-danger" @click="removeTodoDone">清除已完成任务</button></div>
</template>
<script >export default {name: "MyFooter",props:['todos','changeTodoDones','clearTodoDone'],computed:{total(){const a = this.todos.length;return a>0 ? true : false;},totalDone(){const number = this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0);return number;},isAll:{get(){return this.totalDone === this.todos.length && this.todos.length > 0;},set(value){this.changeTodoDones(value);}},},methods:{removeTodoDone(){this.clearTodoDone();}}}
</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>
5、App.vue
<template><div id="app"><div class="todo-container"><div class="todo-wrap"><my-header :addTodo="addTodo"></my-header><my-list :todos="todos" :changeDone="changeDone" :deleteToto="deleteToto"></my-list><my-footer :todos="todos" :changeTodoDones="changeTodoDones" :clearTodoDone="clearTodoDone"></my-footer></div></div></div>
</template>
<script>
import MyHeader from './components/MyHeader'
import MyFooter from './components/MyFooter'
import MyList from './components/MyList'export default {name: 'App',components: {MyHeader,MyList,MyFooter},data(){return{todos:[{id:'001',title:'吃饭',done:true},{id:'002',title:'睡觉',done:false},{id:'003',title:'喝酒',done:true}]}},methods:{addTodo(obj){this.todos.unshift(obj);},changeDone(id){this.todos.forEach((p)=>{if(p.id === id){p.done = !p.done}})},deleteToto(id){this.todos=this.todos.filter(p=>p.id !== id);},changeTodoDones(value){this.todos.forEach(p=>{p.done=value;});},clearTodoDone(){this.todos=this.todos.filter(p=>{return !p.done})}}
}
</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;}
</style>
6、main.js
//该文件是整个项目的入口文件
//引入Vue
import Vue from 'vue'
//引入APP组件,他是所有组件的父组件
import App from './App.vue'//关闭Vue是生产提示
Vue.config.productionTip = false;
//应用插件//创建Vue实例对象---vm
new Vue({render: h => h(App),
}).$mount('#app');
7、总结