1、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:JSON.parse(localStorage.getItem('todos')) || []
}
},
watch:{
todos:{
deep:true,
handler(newvalue) {
localStorage.setItem('todos',JSON.stringify(newvalue));
}
}
},
methods:{
addTodo(obj){
// console.log(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){
console.log(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>
2、MyFooter.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'],
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);
this.$emit('changeTodoDones',value)
}
},
},
methods:{
removeTodoDone(){
this.$emit('clearTodoDone');
//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>
3、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",
data(){
return{
title:''
}
},
methods:{
add(){
if(!this.title.trim()) alert("输入不能为零") ;
const obj = {id:nanoid(),title: this.title,done:false};
//this.addTodo(obj);
this.$emit('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>