一、运行效果
组件结构
采用 “父子组件” 分层设计,App 作为根组件统一管理数据,子组件负责具体功能,结构如下:
App(父组件)→ 管理核心数据items,提供操作方法
├─ MyHeader(子组件)→ 任务添加
├─ MyList(子组件)→ 任务列表容器
│ └─ MyItem(子组件)→ 单个任务项(渲染+删除+状态切换)
└─ MyFooter(子组件)→ 全选+统计+清除已完成任务
二、完整代码实现
1. 入口文件:main.js
//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false
Vue.prototype.$bus = new Vue()
//创建vm
new Vue({
el:'#app',
render: h => h(App)
})
2. 根组件:App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<MyHeader @addItem="addItem"/>
<MyList :items="items" />
<MyFooter :items="items" @changeAllCheck="changeAllCheck" @del="del"/>
</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:{
MyFooter,
MyHeader,
MyList
},
data() {
return {
items: JSON.parse(localStorage.getItem('items')) || [] //第一次调用localStorage.getItem('items')为null
}
},
methods:{
addItem(item){
this.items.unshift(item)
},
changeCheck(id){
this.items.forEach(item => {
if(item.id === id) item.isChecked = !item.isChecked
});
},
del(id){
this.items = this.items.filter(item=>item.id!==id)
},
update(id,name){
this.items.forEach(item => {
if(item.id === id) item.name = name
});
},
changeAllCheck(isChecked){
this.items.forEach(item => {
item.isChecked = isChecked
});
}
},
watch:{
items:{
deep:true,
handler(val){
localStorage.setItem('items',JSON.stringify(val))
}
}
},
mounted() {
this.$bus.$on('changeCheck',this.changeCheck)
this.$bus.$on('del',this.del)
this.$bus.$on('update',this.update)
},
beforeDestroy(){
this.$bus.$off(['changeCheck','del','update'])
}
}
</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-primary {
color: #fff;
background-color: skyblue;
border: 1px solid skyblue;
}
.btn-primary:hover {
color: #fff;
background-color: skyblue;
}
.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>
3. 任务添加组件:MyHeader.vue
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="name" @keyup.enter="addItem"/>
</div>
</template>
<script>
export default {
name:'MyHeader',
data() {
return {
name:''
}
},
props:[''],
methods:{
addItem(){
if(!this.name.trim()) return alert('输入不能为空')
const item = {id:crypto.randomUUID(),name:this.name,isChecked:false}
this.$emit('addItem',item)
this.name = ''
}
}
}
</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>
4. 任务列表容器:MyList.vue
<template>
<ul class="todo-main">
<MyItem v-for="
item in items"
:key="item.id"
:item="item"
/>
</ul>
</template>
<script>
import MyItem from './MyItem.vue';
export default {
name:'MyList',
components:{
MyItem
},
props:['items']
}
</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>
5. 单个任务项组件:MyItem.vue
<template>
<div>
<li >
<label >
<input type="checkbox" :checked="item.isChecked" @change="changeCheck"/>
<span>{{item.name}}</span>
</label>
<button class="btn btn-danger" @click="del">删除</button>
<button class="btn btn-primary" @click="toUpdate" >编辑</button>
</li>
<div v-show="showEditModal" class="modal">
<div class="modal-content">
<span>修改内容:</span>
<input type="text" v-model="name" ref="input" @keyup.enter="update"/>
<div class="bts">
<button class="btn btn-primary" @click="update">保存</button>
<button class="btn btn-danger" @click="showEditModal = false">取消</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name:'MyItem',
data() {
return {
showEditModal:false,
name:this.item.name
}
},
props:['item'],
methods:{
toUpdate(){
this.showEditModal =true
/*
this.$refs.input.focus()
存在问题:Vue异步更新DOM可能导致<input/>未创建,直接调用focus()报错
解决方案:使用$nextTick(function)。即在下一次dom更新循环完成后执行回调函数以确保input已生成
*/
this.$nextTick(() => {
this.$refs.input.focus();
})
}
,
update(){
this.$bus.$emit('update',this.item.id,this.name)
this.showEditModal = false
},
handleDel(id){
if(confirm('确定删除?'))
this.$bus.$emit('del',id)
},
changeCheck(){
this.$bus.$emit('changeCheck',this.item.id)
},
del(){
if(confirm('确定删除?'))
this.$bus.$emit('del',this.item.id)
}
}
}
</script>
<style scoped>
/* 简单的弹窗样式 */
.modal {
position: fixed;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
.bts{
justify-content: center; /* 水平居中 */
margin: 0 20px;
}
.bts .btn{
margin-top: 20px;
margin-left: 20px;
margin-right: 20px;
}
/*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{
background-color: #ddd;
}
li:hover button{
display: block;
}
</style>
6. 底部统计组件:MyFooter.vue
实现全选 / 取消全选、已完成任务统计、清除已完成任务功能,通过computed
计算全选状态和任务数量。
<template>
<div class="todo-footer" v-show="total">
<label>
<input type="checkbox" v-model="isAll"/>
</label>
<span>
<span>已完成{{ checkNum }}</span> / 全部{{ total }}
</span>
<button class="btn btn-danger" @click="clearFinishedItems">清除已完成任务</button>
</div>
</template>
<script>
export default {
name:'MyFooter',
props:['items'],
computed:{
isAll:{
get(){
return this.checkNum === this.total && this.total > 0
},
set(val){
this.$emit('changeAllCheck',val)
}
},
total(){
return this.items.length
},
checkNum(){
return this.items.filter(item=>item.isChecked).length
}
},
methods: {
clearFinishedItems(){
this.items.forEach(item => {
if(item.isChecked)
this.$emit('del',item.id)
});
}
},
}
</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>