基于Vue写todoList列表

本文介绍如何使用Vue.js框架创建一个简单的待办事项应用程序,包括输入、保存、删除和标记任务为已完成的功能,并利用本地存储实现数据持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>

        *{

            margin: 0;

            padding: 0;

            box-sizing: border-box;

        }

 

    .box{

        width: 500px;

        margin: 0 auto;

        border: 1px greenyellow solid;

    }

    #app{

        margin-top: 20px;

    }

    #app input{

        display: inline-block;

        width: 400px;

        height: 50px;

    }

    .thing{

       background-color: khaki;

       text-align: center;

       height: 50px;

       border-bottom: 1px solid white;

 

    }

    .thing p{

        background-color: lightgreen;

        /* margin-top: 10px; */

        width: 400px;

        height: 50px;

        float: left;

        line-height: 50px;

        border-bottom: 1px solid white;

 

    }

    #app h3{

        text-align: center;

        margin-top: 20px;

    }

    button{

        display: inline-block;

        width: 40px;

        height: 30px;

        margin-top: 10px;

    }

    .complete{

       background-color: khaki;

       text-align: center;

       height: 50px;

       border-bottom: 1px solid white;

    }

    .complete p{

        background-color: lightgreen;

        /* margin-top: 10px; */

        width: 400px;

        height: 50px;

        float: left;

        line-height: 50px;

        border-bottom: 1px solid white;

        text-decoration: line-through;

    }

    </style>

</head>

<body>

    <div class="box">

        <div id="app">

            <input type="text" v-model="message" placeholder="请输入您的代办事项">

            <button v-on:click="myClick">保存</button>

            <h3>待办事项</h3>

            <div  class="thing" v-for="(item,index) in list">

                <p >{{item}}</p>

                <button @click="thingCom(index)">完成</button>

                <button @click="delItemthing(index)">删除</button>

            </div>

            <h3>完成事项</h3>

            <div  class="complete" v-for="(item,index) in comlist">

                <p >{{item}}</p>

                <button @click="delItemcom(index)">删除</button>

            </div>

        </div>

    </div>

</body>

<script>

    var app = new Vue({

        el:"#app",

        data:{

            message:"",

            list:[],

            comlist:[]

        },

        methods:{

            myClick:function(){

                if(this.message!=""){

                    if((this.list).indexOf((this.message).trim())==-1 &&this.message!="" ){

                     this.list.push(this.message.trim())

                 }

                 else{

                     alert("不能重复,")

                 }

                }

                else{

                    alert("不能为空")

                }

                this.message =""

                localStorage.setItem("list",JSON.stringify(this.list))

            },

            thingCom(index){

                app.comlist.push(this.list[index])

                app.list.splice(index,1)

                localStorage.setItem("list",JSON.stringify(this.list))

                localStorage.setItem("comlist",JSON.stringify(this.comlist))

            },

 

            delItemthing(index){

                app.list.splice(index,1)

                localStorage.setItem("list",JSON.stringify(this.list))

 

            },

            delItemcom(index){

                app.comlist.splice(index,1)

                localStorage.setItem("comlist",JSON.stringify(this.comlist))

                console.log(app.comlist)

            }

        },

        created(){

            let a =localStorage.getItem("list")

            if(a){

                a=JSON.parse(a)

                this.list =a

            };

            let b=localStorage.getItem("comlist")

            if(b){

                b=JSON.parse(b)

                this.comlist=b

            }

        }

    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值