vue 两个列表之间的添加和删除


实现从a列表添加数据到b列表,b列表可删除数据

<template>
  <div class="home">
    <div style="display: flex;flex-direction: row">
      <div style="flex: 1">
        <div
          class="div"
          v-for="(item, index) in list"
          :key="index"
          @click="currentClick(index)"
          :class="{ active: currentNum === index }"
        >
          {
  { item }}
        </div>
      </div>
      <div style="flex: 1">
        <button @click="addList()" class="butn">添加</button>
        <button @click=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 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、付费专栏及课程。

余额充值