当前位置:首页 > VUE

vue实现增删改功能

2026-01-22 00:30:56VUE

Vue 实现增删改功能

在 Vue 中实现增删改功能通常涉及数据绑定、事件处理和状态管理。以下是一个完整的实现方案,涵盖增删改查(CRUD)的核心逻辑。

数据初始化

在 Vue 组件的 data 中定义初始数据列表和表单对象:

data() {
  return {
    items: [
      { id: 1, name: 'Item 1' },
      { id: 2, name: 'Item 2' }
    ],
    form: {
      id: null,
      name: ''
    },
    editing: false
  }
}

新增功能

通过表单绑定 v-model 并触发添加方法:

<input v-model="form.name" placeholder="Enter name">
<button @click="addItem">Add</button>
methods: {
  addItem() {
    if (!this.form.name) return
    this.items.push({
      id: Math.max(...this.items.map(i => i.id)) + 1,
      name: this.form.name
    })
    this.resetForm()
  },
  resetForm() {
    this.form = { id: null, name: '' }
    this.editing = false
  }
}

删除功能

通过索引或唯一标识符删除数据:

<ul>
  <li v-for="(item, index) in items" :key="item.id">
    {{ item.name }}
    <button @click="deleteItem(index)">Delete</button>
  </li>
</ul>
methods: {
  deleteItem(index) {
    this.items.splice(index, 1)
  }
}

编辑功能

实现编辑需要先填充表单再更新数据:

<button @click="editItem(item)">Edit</button>
methods: {
  editItem(item) {
    this.form = { ...item }
    this.editing = true
  },
  updateItem() {
    const index = this.items.findIndex(i => i.id === this.form.id)
    this.items.splice(index, 1, { ...this.form })
    this.resetForm()
  }
}

条件渲染提交按钮

根据编辑状态切换按钮功能:

vue实现增删改功能

<button @click="editing ? updateItem() : addItem()">
  {{ editing ? 'Update' : 'Add' }}
</button>

完整组件示例

<template>
  <div>
    <input v-model="form.name" placeholder="Enter name">
    <button @click="editing ? updateItem() : addItem()">
      {{ editing ? 'Update' : 'Add' }}
    </button>

    <ul>
      <li v-for="(item, index) in items" :key="item.id">
        {{ item.name }}
        <button @click="editItem(item)">Edit</button>
        <button @click="deleteItem(index)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      form: {
        id: null,
        name: ''
      },
      editing: false
    }
  },
  methods: {
    addItem() {
      if (!this.form.name) return
      this.items.push({
        id: Math.max(...this.items.map(i => i.id)) + 1,
        name: this.form.name
      })
      this.resetForm()
    },
    editItem(item) {
      this.form = { ...item }
      this.editing = true
    },
    updateItem() {
      const index = this.items.findIndex(i => i.id === this.form.id)
      this.items.splice(index, 1, { ...this.form })
      this.resetForm()
    },
    deleteItem(index) {
      this.items.splice(index, 1)
    },
    resetForm() {
      this.form = { id: null, name: '' }
      this.editing = false
    }
  }
}
</script>

关键点说明

  • 数据驱动:所有操作直接修改 items 数组,Vue 自动更新 DOM
  • 唯一标识:使用 id 确保操作准确性,避免直接依赖数组索引
  • 表单复用:通过 editing 状态切换添加/编辑模式
  • 响应式更新:数组方法如 push/splice 会触发视图更新

对于复杂场景,建议使用 Vuex 或 Pinia 管理状态,组件间通过 props/events 通信。

标签: 功能vue
分享给朋友:

相关文章

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue实现折叠

vue实现折叠

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

自己实现vue

自己实现vue

实现简易版 Vue.js 核心功能 要实现一个简易版 Vue.js,需要理解其核心功能:数据响应式、模板编译、依赖收集和虚拟 DOM。以下分模块实现关键功能。 数据响应式(Reactivity) 通…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…