vue 简单案例 todolist

本文通过一个简单的 Vue.js TodoList 案例,介绍 Vue 的核心特性,包括数据绑定、指令使用、组件化等,帮助初学者快速上手 Vue 开发。

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

<template>
  <div id="app">
  <div class="title">
    <span class="text">TODOList</span>
    <input placeholder="请输入..." type="text" class="txtbox" @keydown="addList">
  </div>

  <div>
    <p class="notdo">未录入名单</p>
    <ul>
        <li  v-if="!res.ckof"  v-for="(res,index) in list":key="index">
        <input type="checkbox" @click="checkof(index)":checked="res.ckof" > {{res.name}}-----<button @click="cancel(index)">x</button>
      </li>
    </ul>
    
    <p class="cando">已录入名单</p>
    <ul>
       <li v-if="res.ckof" v-for="(res,index) in list":key="index">
       <input type="checkbox" @click="checkof(index)":checked="res.ckof">{{res.name}};-----<button @click="cancel(index)">x</button>
       </li>
    </ul>
  </div>
  
    
  </div>
</template>

<script>
import storage from "./storage.js";
export default {
  name: 'App',
  data(){
    return {
      list:[
        {
          name:"张三",
          ckof:false,
        },
        
        {
          name:"李四",
          ckof:true,
        }
      ]
    }
  },
  mounted(){//组件挂载完成 生命周期钩子函数
  this.list=storage.getStorage("list");
  },
  methods:{
    
    //添加新栏目的方法
    addList(e){
      if(e.keyCode==13){
          let a=e.target.value;
          if(a!=""){
            this.list.push({
              name:a,
              ckof:false,
              });
             a="";
             storage.setStorage("list",JSON.stringify(this.list));
             
          }
      }
    },
    checkof(index){
      //console.log(this)=>VueComponent;
       this.list[index].ckof=!this.list[index].ckof;
       storage.setStorage("list",JSON.stringify(this.list));
    },
    cancel(key){
      this.list.splice(key,1);
       storage.setStorage("list",JSON.stringify(this.list));
    }
  }
  


  
}
</script>

<style>
let storage={
    setStorage(key,value){
        localStorage.setItem(key,value);
    },
    getStorage(key){
       return JSON.parse(localStorage.getItem(key));
    }
};
export default storage;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值