当前位置:首页 > 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()
  }
}

条件渲染提交按钮

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

<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 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue实现名字查找功能

vue实现名字查找功能

Vue 实现名字查找功能 在 Vue 中实现名字查找功能,可以通过以下方法完成。这里假设有一个名字列表,需要根据用户输入的关键词进行筛选。 数据准备 在 Vue 的 data 中定义名字列表和搜索关…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-toa…