vue中 简单的添加删除功能实现

<!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="./vue.js"></script>
  <link rel="stylesheet" href="./index.css">
</head>
<body>

  <div id="app">
    <div class="header">
      <div class="container">
        <div class="logo">toDoList</div>
        <div class="input-area">
          <input type="text" @input="handleInput" :value="curMask"/>
          <button @click="addMask">添加</button>
        </div>
      </div>
    </div>
    <div class="container">
      <h2>
        <span>正在进行</span>
        <span class="mask-num">{{ needDoList.length }}</span>
      </h2>
      <ul class="mask-list">
        <li
          v-for="(mask, index) in needDoList"
          :key="mask.id"
        >
          <div>
            <input type="checkbox" @change="doCheck(index, 'need')"/>
            <span>{{ mask.title }}</span>
          </div>
          <button @click="deleteMask(index, 'need')">删除</button>
        </li>
      </ul>
    </div>
    <div class="container">
      <h2>
        <span>已经完成</span>
        <span class="mask-num">{{ completeList.length }}</span>
      </h2>
      <ul class="mask-list complete-list">
        <li
          v-for="(mask, index) in completeList"
          :key="mask.id"
        >
          <div>
            <input type="checkbox" @change="doCheck(index, 'complete')"/>
            <span>{{ mask.title }}</span>
          </div>
          <button @click="deleteMask(index, 'complete')">删除</button>
        </li>
      </ul>
    </div>
  </div>

  <script>

    const vm = new Vue({
      el: '#app',
      data: {
        curMask: '',
        needDoList: [],   //接收   正在进行的数组
        completeList: []   //已经完成
      },
      methods: {
        handleInput (e) {
          this.curMask = e.target.value;   //获取input的值
        },
        addMask () {
          //this.needDoList.push(this.curMask)
          if(this.curMask === '') { return };   //如果输入的input值为空 直接返回
          this.needDoList.push({    //点击按钮 将input的值添加到 正在进行noodDoList的数组当中
            title: this.curMask,    //添加的是字符串  字符串没有title和对象  需要转化为对象
            id: Math.random()
          });
          this.curMask = '';     //按钮添加之后  input为空
        },
        doCheck (index, type) {

          if(type === 'need') {     //点击checkbox ,正在进行变为已经完成  为了区分正在进行和已经完成,设置type
            const completeMask = this.needDoList.splice(index, 1);
            this.completeList.push(...completeMask);
          } else {
            const noCompleteMask = this.completeList.splice(index, 1);
            this.needDoList.push(...noCompleteMask);
          }
        },
        deleteMask (index, type) {    //已经完成和正在进行 都是点击删除按钮进行删除  为了区分 设置type
          const toDoList = type === 'need' ? this.needDoList : this.completeList;  //如果type=need 则删除是needDoList  否则删除的是completeList
          toDoList.splice(index, 1);
        }
      }
    })



   
    
  </script>
</body>
</html>
好的,下面是一个简单Vue 实现 todolist 的添加删除功能的代码示例: ```html <!DOCTYPE html> <html> <head> <title>Vue TodoList</title> <script src="https://cdn.jsdelivr.net/npm/vue"></script> </head> <body> <div id="app"> <h1>TodoList</h1> <input type="text" v-model="newTodo"> <button @click="addTodo">Add</button> <ul> <li v-for="(todo, index) in todos"> {{ todo }} <button @click="removeTodo(index)">Delete</button> </li> </ul> </div> <script> var app = new Vue({ el: '#app', data: { newTodo: '', todos: [] }, methods: { addTodo: function() { if (this.newTodo != '') { this.todos.push(this.newTodo); this.newTodo = ''; } }, removeTodo: function(index) { this.todos.splice(index, 1); } } }) </script> </body> </html> ``` 在这个示例,我们首先定义了一个包含两个属性的 Vue 实例,分别是 `newTodo` 和 `todos`。`newTodo` 表示用户输入的待办事项,`todos` 表示所有已添加的待办事项列表。 然后,我们定义了两个方法,`addTodo` 和 `removeTodo`,来实现添加删除待办事项的功能。在 `addTodo` 方法,我们首先判断用户输入的待办事项是否为空,如果不为空,则将其添加到 `todos` 列表,并清空 `newTodo`。在 `removeTodo` 方法,我们通过 `splice` 方法从 `todos` 列表删除指定索引的待办事项。 最后,我们在页面使用 `v-model` 指令绑定用户输入的待办事项到 `newTodo` 属性上,使用 `v-for` 指令循环渲染 `todos` 列表,并在每个待办事项后面添加一个删除按钮,通过 `@click` 绑定 `removeTodo` 方法来实现删除功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值