当前位置:首页 > VUE

vue实现动态添加删除

2026-02-24 02:15:54VUE

Vue 动态添加和删除的实现方法

使用 v-for 和数组方法

在 Vue 中可以通过 v-for 指令结合数组方法来动态渲染列表,并通过数组的 push 和 splice 方法实现添加和删除功能。

<template>
  <div>
    <button @click="addItem">添加项目</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['项目1', '项目2', '项目3']
    }
  },
  methods: {
    addItem() {
      this.items.push(`项目${this.items.length + 1}`)
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

使用计算属性

对于更复杂的动态内容,可以使用计算属性来管理数据,并通过方法来修改数据源。

vue实现动态添加删除

<template>
  <div>
    <input v-model="newItem" placeholder="输入新项目">
    <button @click="addItem">添加</button>
    <ul>
      <li v-for="(item, index) in filteredItems" :key="index">
        {{ item }}
        <button @click="removeItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      newItem: ''
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => item.length > 0)
    }
  },
  methods: {
    addItem() {
      if (this.newItem.trim()) {
        this.items.push(this.newItem)
        this.newItem = ''
      }
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

动态组件

对于需要动态添加和删除组件的情况,可以使用动态组件配合 is 属性。

vue实现动态添加删除

<template>
  <div>
    <button @click="addComponent">添加组件</button>
    <div v-for="(comp, index) in components" :key="index">
      <component :is="comp.type" @remove="removeComponent(index)"></component>
    </div>
  </div>
</template>

<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'

export default {
  components: { ComponentA, ComponentB },
  data() {
    return {
      components: [],
      nextComponent: 0
    }
  },
  methods: {
    addComponent() {
      this.components.push({
        type: this.nextComponent % 2 === 0 ? 'ComponentA' : 'ComponentB'
      })
      this.nextComponent++
    },
    removeComponent(index) {
      this.components.splice(index, 1)
    }
  }
}
</script>

注意事项

为每个动态生成的元素添加唯一的 key 属性,帮助 Vue 高效地更新 DOM。

避免直接操作 DOM,始终通过修改数据来驱动视图变化。

对于大型列表,考虑使用虚拟滚动技术优化性能。

标签: 动态vue
分享给朋友:

相关文章

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 get…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…