todo-list案例(尚硅谷)

1.拆分组件

按照功能点拆分

  • TodoHeader
  • TodoList
  • TodoItem
  • TodoFooter

2.静态组价拆分编写

App.vue

<template>
  <div id="root">
    <div class="Todo-container">
      <div class="Todo-wrap">
        <Todo-header></Todo-header>
        <Todo-list></Todo-list>
        <Todo-footer></Todo-footer>
      </div>
    </div>
  </div>
</template>

<script>
  import TodoList from "./components/TodoList"
  import TodoHeader from "./components/TodoHeader"
  import TodoFooter from "./components/TodoFooter"
  
  export default {
    name:"App",
    components:{
      TodoList,
      TodoFooter,
      TodoHeader
    },
  }
</script>

<style>
  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>

TodoHeader.vue

<template>
  <div class="Todo-header">
		<input type="text" placeholder="请输入你的任务名称,按回车键确认"/>
	</div>
</template>

<script>
  export default {
    name:"TodoHeader"
  }
</script>

<style scoped>
  .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>

TodoList.vue

<template>
  <ul class="Todo-main">
		<Todo-item></Todo-item>
		<Todo-item></Todo-item>
		<Todo-item></Todo-item>
		<Todo-item></Todo-item>
	</ul>
</template>


<script>
  import TodoItem from '@/components/TodoItem'

  export default {
    name:"TodoList",
    components:{TodoItem}
  }
</script>

<style scoped>
  .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>

TodoItem.vue

<template>
  <li>
		 <label>
				<input type="checkbox"/>
				<span>xxxxx</span>
		 </label>
			  <button class="btn btn-danger" style="display:none">删除</button>
	</li>
</template>

<script>
  export default {
    name:"TodoItem"
  }
</script>

<style scoped>
  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;
	}   
</style>

TodoFooter.vue

<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox">
    </label>
    <span>
      <span>已完成0 </span> / 全部4
    </span>
    <button class="btn btn-danger">清除已完成任务</button>
  </div>
</template>

<script>
  export default {
    name:"TodoFooter"
  }
</script>
 
<style scoped>
  .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.初始化列表

TodoList.vue

<template>
  <ul class="Todo-main">
	<Todo-item v-for="todoObj in todos" :key="todoObj.id" :todoObj="todoObj"></Todo-item>
  </ul>
</template>

<script>
	export default{
		data(){
		      return {
		        todos:[
		          {id:"01",name:"吃饭",done:true},
		          {id:"02",name:"睡觉",done:true},
		          {id:"03",name:"学习",done:false},
		          {id:"04",name:"开车",done:true},
		        ]
		      }
		    }
		 }
	......
</script>

TodoItem.vue

<script>
<template>
  <li>
	<label>
		<input type="checkbox" :checked="todoObj.done"/>
		<span>{{todoObj.name}}</span>
	</label>
	<button class="btn btn-danger" style="display:none">删除</button>
  </li>
</template>
  export default {
    name:"TodoItem",
    props:["todoObj"]
  }
  ......
</script>

3.添加一个todo

由于目前我们所学的知识不能完成兄弟组件进行传值,因此我们将兄弟组件所需要用到的数据需要用到的数据放入他们共同的父组件中,进而进行传值,既可以传data中的数据,也可以传方法

父组件App.vue,存放数据todos

<template>
  <div id="root">
    <div class="Todo-container">
      <div class="Todo-wrap">
        <Todo-header :addTodo="addTodo"></Todo-header>
        <Todo-list :todos="todos"></Todo-list>
        <Todo-footer></Todo-footer>
      </div>
    </div>
  </div>
</template>

<script>
  import TodoList from "./components/TodoList"
  import TodoHeader from "./components/TodoHeader"
  import TodoFooter from "./components/TodoFooter"
  
  export default {
    name:"App",
    components:{
      TodoList,
      TodoFooter,
      TodoHeader
    },
    data(){
      return {
        todos:[
          {id:"01",name:"吃饭",done:true},
          {id:"02",name:"睡觉",done:true},
          {id:"03",name:"学习",done:false},
          {id:"04",name:"开车",done:true},
        ]
      }
    },
    methods:{
      addTodo(todoObj){
        this.todos.unshift(todoObj)
        console.log(todoObj);
      }
    }
  }
</script>
<style>
......
</style>

子组件TodoList.vue通过props接收数据

<template>
  <ul class="Todo-main">
		<Todo-item v-for="todoObj in todos" :key="todoObj.id" :todoObj="todoObj"></Todo-item>
	</ul>
</template>


<script>
  import TodoItem from '@/components/TodoItem'

  export default {
    name:"TodoList",
    components:{TodoItem},
    data(){
      return {
      }
    },
    props:["todos"]
  }
</script>

子组件TodoHeader.vue通过props接收父组件App.vue传过来的方法,在适当时机可以直接调用父组件App.vue的方法,并且可以在方法中传值

<template>
	<div class="Todo-header">
		<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="name" @keyup.enter="add"/>
	</div>
</template>

<script>
import {nanoid} from "nanoid"

export default {
	name: "TodoHeader",
	data() {
		return {
			name: "",
		};
	},
	props:["addTodo"],
	methods:{
		add(){
			// 将用户的输入包装成一个TODO对象
			const todoObj={id:nanoid(),name:this.name,done:false}
			this.addTodo(todoObj)
		}
	}
};
</script>

3.勾选or取消todo

在App.vue中添加一个方法,然后将其通过属性先传给TodoList.vue,再通过TodoList传给TodoItem.vue

App.vue中添加方法:

 // 取消or勾选一个todo
      checkTodo(id){
        this.todos.forEach(item => {
          if(item.id==id){
            item.done=!item.done
          }
        });
      }

最终在TodoItem.vue中通过props接收到该方法,当勾选状态发生改变时,触发父组件该方法,并且将id值传过去

TodoItem.vue:

<template>
	<li>
	 <label>
		<input type="checkbox" :checked="todoObj.done" 	@change="handleCheck(todoObj.id)"/>
		<span>{{todoObj.name}}</span>
	 </label>
	 <button class="btn btn-danger" style="display:none">删除</button>
	</li>
</template>

<script>
	export default {
		name:"TodoItem",
		props:["todoObj","checkTodo"],
		methods:{
			handleCheck(id){
				this.checkTodo(id)
			}
		}
	}
</script>

3.初版本代码

App.vue

TodoList.vue

<template>
  <ul class="Todo-main">
		<Todo-item v-for="todoObj in todos" :key="todoObj.id" :todoObj="todoObj" 
     :checkTodo="checkTodo" :deleteTodo="deleteTodo">
    </Todo-item>
	</ul>
</template>


<script>
  import TodoItem from '@/components/TodoItem'

  export default {
    name:"TodoList",
    components:{TodoItem},
    data(){
      return {
      }
    },
    props:["todos","checkTodo","deleteTodo"]
  }
</script>

<style scoped>
  .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>

TodoItem.vue

<template>
	<li>
		 <label>
				<input type="checkbox" :checked="todoObj.done" @change="handleCheck(todoObj.id)"/>
				<span>{{todoObj.name}}</span>
		 </label>
		 <button class="btn btn-danger" @click="handleDelete(todoObj.id)">删除</button>
	</li>
</template>

<script>
	export default {
		name:"TodoItem",
		props:["todoObj","checkTodo","deleteTodo","deleteTodo"],
		methods:{
			handleCheck(id){
				this.checkTodo(id)
			},
			handleDelete(id){
				this.deleteTodo(id)
			}
		}
	}
</script>

<style scoped>
	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:hover{
		background-color: #e2e2e2;
	}

	li:hover button{
		display: block;
	}

	li button {
		float: right;
		display: none;
		margin-top: 3px;
	}

	li:before {
		content: initial;
	}

	li:last-child {
		border-bottom: none;
	}   
</style>

TodoHeader.vue

<template>
	<div class="Todo-header">
		<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="name" @keyup.enter="add"/>
	</div>
</template>

<script>
import {nanoid} from "nanoid"

export default {
	name: "TodoHeader",
	data() {
		return {
			name: "",
		};
	},
	props:["addTodo"],
	methods:{
		add(){
			// 将用户的输入包装成一个TODO对象
			const todoObj={id:nanoid(),name:this.name,done:false}
			this.addTodo(todoObj)
		}
	}
};
</script>

<style scoped>
.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>

TodoFooter.vue

<template>
  <div class="todo-footer" v-show="totalTodo">
    <label>
      <input type="checkbox" v-model="checkAll">
    </label>
    <span>
      <span>已完成{{doneTotal}} </span> / 全部{{totalTodo}}
    </span>
    <button class="btn btn-danger" @click="clearDone">清除已完成任务</button>
  </div>
</template>

<script>
  export default {
    name:"TodoFooter",
    props:["todos","checkAllTodo","clearDoneTodo"],
    computed:{
      totalTodo(){
        return this.todos.length
      },
      doneTotal(){
        return this.todos.reduce((pre,current)=>{
          return current.done?pre+1:pre
        },0)
      },
      checkAll:{
        get(){
          return this.totalTodo==this.doneTotal&&this.totalTodo>0
        },
        set(value){
          this.checkAllTodo(value)
        }
      }
    },
    methods:{
      clearDone(){
        this.clearDoneTodo()
      }
    }
  }
</script>
 
<style scoped>
  .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.最终代码

todo-list案例资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姜鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值