IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 《Vue学习》尚硅谷Vue Todo_List案例集锦 -> 正文阅读

[JavaScript知识库]《Vue学习》尚硅谷Vue Todo_List案例集锦

该篇整理了尚硅谷Vue中P70~P76TodoLists的代码,希望能够便于大家的学习

P70静态网页?

index.js

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <!-- 针对IE浏览器的一个特殊配置,含义是让IE浏览器以最高的渲染级别界面 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 开启移动端理想视口 -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <!-- link图标 <%= BASE_URL %>指的是public的路径-->
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <!-- 配置网页的标题 Webpack打包-->
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <!-- 不支持Js,标签中的元素会处理 -->
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

MyHeader.vue

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

<script>
    export default {
        name:'MyHeader',
    }
</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>

MyFooter.vue?

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

<script>
    export default {
        name:'MyFooter',
        
    }
</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>

?MyList.vue

<template>
    <ul class="todo-main">
        <MyItem/>
        <MyItem/>
        <MyItem/>
        <MyItem/>
    </ul>
</template>

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem}
    }
</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>

MyItem.vue

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

<script>
    export default {
        name:'MyItem',
        
    }
</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>

App.vue?

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader/>
                <MyList/>
                <MyFooter/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                msg:'欢迎学习Vue'
            }
        },
    }
</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>

?效果图

P71初始化列表

MyHeader.vue

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

<script>
    export default {
        name:'MyHeader',
    }
</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>

MyFooter.vue

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

<script>
    export default {
        name:'MyFooter',
        
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        data(){
            return {
                todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}


                ]
            }
        }
    }
</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>

MyItem.vue

<template>
   <div>
       <li>
        <label >
            <input type="checkbox" :checked="todo.done"/>
            <span>{{todo.title}}</span>
        </label>
        <button class="btn btn-danger" style="display:none">删除</button>
       </li>
   </div>
</template>

<script>
    export default {
        name:'MyItem',
        props:['todo']
    }
</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>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader/>
                <MyList/>
                <MyFooter/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                msg:'欢迎学习Vue'
            }
        },
    }
</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>

效果图?

P72添加

MyHeader.vue

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

<script>
    import {nanoid} from 'nanoid'
    export default {
        name:'MyHeader',
        data(){
            return {
                title:''
            }
        },
        props:['addTodo'],
        methods:{
            add()
            {
                if(!this.title.trim())return alert("输入不能为空")
                const todoObj={id:nanoid(),title:this.title,done:false}
                this.addTodo(todoObj)
                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>

MyFooter.vue

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

<script>
    export default {
        name:'MyFooter',
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        props:['todos']
    }
</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>

MyItem.vue

<template>
   <div>
       <li>
        <label >
            <input type="checkbox"  />
            <span>{{todo.title}}</span>
        </label>
        <button class="btn btn-danger" style="display:none">删除</button>
       </li>
   </div>
</template>

<script>
    export default {
        name:'MyItem',
        props:['todo'],
        
    }
</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>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader :addTodo="addTodo"/>
                <MyList :todos="todos"/>
                <MyFooter/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyFooter from './components/MyFooter'
    import MyHeader from './components/MyHeader'
    import MyList from './components/MyList.vue'
   
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList},
        data(){
            return {
                todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}
                ]
            }
        },
        methods:{
            addTodo(todoObj)
            {
                this.todos.unshift(todoObj)
            }
            
        }
    }
</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>

效果图?

?

P73勾选

?MyHeader.vue

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

<script>
    import {nanoid} from 'nanoid'
    export default {
        name:'MyHeader',
        data(){
            return {
                title:''
            }
        },
        props:['addTodo'],
        methods:{
            add()
            {
                if(!this.title.trim())return alert("输入不能为空")
                const todoObj={id:nanoid(),title:this.title,done:false}
                this.addTodo(todoObj)
                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>

MyFooter.vue

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

<script>
    export default {
        name:'MyFooter',
        
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        props:['todos','checkTodo']
    }
</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>

MyItem.vue

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

<script>
    export default {
        name:'MyItem',
        props:['todo','checkTodo'],
        methods:{
            handleCheck(id)
            {
                //现在是获得了这个id,想办法传给App,因为数据在APP里面,如法炮制Myheader
                this.checkTodo(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 button{
       float: right;
       display: none;
       margin-top: 3px;
   }

   li:before{
       content: initial;
   }
   li:last-child{
       border-bottom: none;
   }
   

</style>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader :addTodo="addTodo"/>
                <MyList :todos="todos" :checkTodo="checkTodo"/>
                <MyFooter/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}
                ]
            }
        },
        methods:{
            addTodo(todoObj)
            {
                this.todos.unshift(todoObj)
            },
            checkTodo(id)
            {
                this.todos.forEach((todo)=>{
                    if(todo.id === id)
                    {
                        todo.done = !todo.done
                    }
                })
            }
        }
    }
</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>

效果图(不能为什么有这个错误!)

P74删除

MyHeader.vue

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

<script>
    import {nanoid} from 'nanoid'
    export default {
        name:'MyHeader',
        data(){
            return {
                title:''
            }
        },
        props:['addTodo'],
        methods:{
            add()
            {
                if(!this.title.trim())return alert("输入不能为空")
                const todoObj={id:nanoid(),title:this.title,done:false}
                this.addTodo(todoObj)
                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>

MyFooter.vue

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

<script>
    export default {
        name:'MyFooter',
        
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        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>

MyItem.vue

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

<script>
    export default {
        name:'MyItem',
        props:['todo','checkTodo','deleteTodo'],
        methods:{
            handleCheck(id)
            {
                //现在是获得了这个id,想办法传给App,因为数据在APP里面,如法炮制Myheader
                this.checkTodo(id)
            },
            handleDelete(id)
            {
                if(confirm("确定要删除吗"))
                {
                    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 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>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader :addTodo="addTodo"/>
                <MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
                <MyFooter/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}
                ]
            }
        },
        methods:{
            addTodo(todoObj)
            {
                this.todos.unshift(todoObj)
            },
            checkTodo(id)
            {
                this.todos.forEach((todo)=>{
                    if(todo.id === id)
                    {
                        todo.done = !todo.done
                    }
                })
            },
            deleteTodo(id)
            {
                 this.todos = this.todos.filter((todo)=>{
                    return todo.id !== id
                })
            }
        }
    }
</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>

?

P75底层统计

MyHeader.vue

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

<script>
    import {nanoid} from 'nanoid'
    export default {
        name:'MyHeader',
        data(){
            return {
                title:''
            }
        },
        props:['addTodo'],
        methods:{
            add()
            {
                if(!this.title.trim())return alert("输入不能为空")
                const todoObj={id:nanoid(),title:this.title,done:false}
                this.addTodo(todoObj)
                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>

MyFooter.vue

<template>
   <div class="todo-footer">
       <label>
           <input type="checkbox"/>
       </label>
       <span>
           <span>已完成{{doneTotal}}</span> / 全部{{this.todos.length}}
       </span>
       <button class="btn btn-danger">清除已完成的任务</button>
   </div>
</template>

<script>
    export default {
        name:'MyFooter',
        props:['todos'],
        computed:{
            doneTotal(){
                let i = 0
                this.todos.forEach((todo)=>{
                    if(todo.done)
                    {
                        i++
                    }
                })
                return i
            }
        }
        
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        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>

MyItem.vue

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

<script>
    export default {
        name:'MyItem',
        props:['todo','checkTodo','deleteTodo'],
        methods:{
            handleCheck(id)
            {
                //现在是获得了这个id,想办法传给App,因为数据在APP里面,如法炮制Myheader
                this.checkTodo(id)
            },
            handleDelete(id)
            {
                if(confirm("确定要删除吗"))
                {
                    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 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>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader :addTodo="addTodo"/>
                <MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
                <MyFooter :todos="todos" />
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}
                ]
            }
        },
        methods:{
            addTodo(todoObj)
            {
                this.todos.unshift(todoObj)
            },
            checkTodo(id)
            {
                this.todos.forEach((todo)=>{
                    if(todo.id === id)
                    {
                        todo.done = !todo.done
                    }
                })
            },
            deleteTodo(id)
            {
                 this.todos = this.todos.filter((todo)=>{
                    return todo.id !== id
                })
            }
        }
    }
</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>

?

P76底部交互

MyHeader.vue

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

<script>
    import {nanoid} from 'nanoid'
    export default {
        name:'MyHeader',
        data(){
            return {
                title:''
            }
        },
        props:['addTodo'],
        methods:{
            add()
            {
                if(!this.title.trim())return alert("输入不能为空")
                const todoObj={id:nanoid(),title:this.title,done:false}
                this.addTodo(todoObj)
                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>

MyFooter.vue

<template>
   <div class="todo-footer" v-show="total">
       <label>
           <input type="checkbox" :checked="isAll" @click="clickbtn"/>
       </label>
       <span>
           <span>已完成{{doneTotal}}</span> / 全部{{total}}
       </span>
       <button class="btn btn-danger" @click="clearTask">清除已完成的任务</button>
   </div>
</template>

<script>
    export default {
        name:'MyFooter',
        props:['todos','checkAll','clearAll'],
        computed:{
            doneTotal(){
                let i = 0
                this.todos.forEach((todo)=>{
                    if(todo.done)
                    {
                        i++
                    }
                })
                return i
            },
            total()
            {
                return this.todos.length
            },
            isAll()
            {
                return this.doneTotal === this.total && this.total>0
            }
            
        },
        methods:{
           clickbtn(e)
           {
               this.checkAll(e.target.checked)
           },
           clearTask()
           {
               if(confirm("确认要清除所有已完成的任务吗"))
               {
                   this.clearAll()
               }
           }
        }
        
    }
</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>

MyList.vue

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

<script>
    import MyItem from '././MyItem.vue'
    export default {
        name:'MyList',
        components:{MyItem},
        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>

MyItem.vue

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

<script>
    export default {
        name:'MyItem',
        props:['todo','checkTodo','deleteTodo'],
        methods:{
            handleCheck(id)
            {
                //现在是获得了这个id,想办法传给App,因为数据在APP里面,如法炮制Myheader
                this.checkTodo(id)
            },
            handleDelete(id)
            {
                if(confirm("确定要删除吗"))
                {
                    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 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>

App.vue

<template>
    <div class="root">
        <div class="todo-container">
            <div class="todo-wrap">
                <MyHeader :addTodo="addTodo"/>
                <MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
                <MyFooter :todos="todos" :checkAll="checkAll" :clearAll="clearAll"/>
            </div>
        </div>
        
        
    </div>
</template>

<script>
    import MyHeader from './components/MyHeader'
    import MyFooter from './components/MyFooter.vue'
    import MyList from './components/MyList.vue'
    import MyItem from './components/MyItem.vue'
    
    export default {
        name:'App',
        components:{MyHeader,MyFooter,MyList,MyItem},
        data(){
            return {
                    todos:[
                    {id:'001',title:"抽烟",done:false},
                    {id:'002',title:"喝酒",done:true},
                    {id:'003',title:"烫头",done:true}
                ]
            }
        },
        methods:{
            addTodo(todoObj)
            {
                this.todos.unshift(todoObj)
            },
            checkTodo(id)
            {
                this.todos.forEach((todo)=>{
                    if(todo.id === id)
                    {
                        todo.done = !todo.done
                    }
                })
            },
            deleteTodo(id)
            {
                 this.todos = this.todos.filter((todo)=>{
                    return todo.id !== id
                })
            },
            checkAll(value)
            {
                if(value)
                {
                    this.todos.forEach((todo)=>{
                        todo.done = true;
                    })
                }else{
                    this.todos.forEach((todo)=>{
                        todo.done = false;
                    })
                }
            },
            clearAll()
            {
                 this.todos = this.todos.filter((todo)=>{
                    return !todo.done
                })
            }
        },
        
    }
</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>

效果图

?底部交互

?

如有错误、疏漏,欢迎大家留言指正,感谢观看

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:20:01  更:2022-03-08 22:23:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:28:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码