当前位置:首页 > 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实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…