当前位置:首页 > VUE

vue里实现增删改查

2026-01-07 05:55:11VUE

在Vue中实现增删改查

数据初始化

在Vue组件的data中定义初始数据和表单绑定的变量:

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

添加功能实现

创建方法处理表单提交,将新项添加到数组中:

methods: {
  addItem() {
    if (!this.form.name || !this.form.description) return

    const newItem = {
      id: Date.now(),
      name: this.form.name,
      description: this.form.description
    }
    this.items.push(newItem)
    this.resetForm()
  }
}

删除功能实现

通过数组过滤方法移除指定项:

vue里实现增删改查

deleteItem(id) {
  this.items = this.items.filter(item => item.id !== id)
}

编辑功能实现

设置编辑状态并填充表单数据:

editItem(item) {
  this.isEditing = true
  this.form = { ...item }
}

更新功能实现

修改现有项并重置表单状态:

vue里实现增删改查

updateItem() {
  const index = this.items.findIndex(item => item.id === this.form.id)
  if (index !== -1) {
    this.items.splice(index, 1, { ...this.form })
    this.resetForm()
    this.isEditing = false
  }
}

表单重置

清空表单数据的方法:

resetForm() {
  this.form = {
    id: null,
    name: '',
    description: ''
  }
}

模板示例

<template>
  <div>
    <form @submit.prevent="isEditing ? updateItem() : addItem()">
      <input v-model="form.name" placeholder="Name">
      <input v-model="form.description" placeholder="Description">
      <button type="submit">{{ isEditing ? 'Update' : 'Add' }}</button>
      <button v-if="isEditing" @click="resetForm">Cancel</button>
    </form>

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

使用Vuex管理状态

对于大型应用,建议使用Vuex集中管理状态:

// store.js
export default new Vuex.Store({
  state: {
    items: []
  },
  mutations: {
    ADD_ITEM(state, item) {
      state.items.push(item)
    },
    DELETE_ITEM(state, id) {
      state.items = state.items.filter(item => item.id !== id)
    },
    UPDATE_ITEM(state, updatedItem) {
      const index = state.items.findIndex(item => item.id === updatedItem.id)
      if (index !== -1) {
        state.items.splice(index, 1, updatedItem)
      }
    }
  },
  actions: {
    addItem({ commit }, item) {
      commit('ADD_ITEM', item)
    },
    deleteItem({ commit }, id) {
      commit('DELETE_ITEM', id)
    },
    updateItem({ commit }, item) {
      commit('UPDATE_ITEM', item)
    }
  }
})

使用axios进行API调用

结合后端API实现完整CRUD:

import axios from 'axios'

methods: {
  async fetchItems() {
    const response = await axios.get('/api/items')
    this.items = response.data
  },
  async addItem() {
    const response = await axios.post('/api/items', this.form)
    this.items.push(response.data)
    this.resetForm()
  },
  async deleteItem(id) {
    await axios.delete(`/api/items/${id}`)
    this.items = this.items.filter(item => item.id !== id)
  },
  async updateItem() {
    const response = await axios.put(`/api/items/${this.form.id}`, this.form)
    const index = this.items.findIndex(item => item.id === this.form.id)
    this.items.splice(index, 1, response.data)
    this.resetForm()
    this.isEditing = false
  }
}

标签: vue
分享给朋友:

相关文章

vue实现滑动

vue实现滑动

Vue 实现滑动效果 使用 CSS 过渡和动画 通过 Vue 的 transition 组件结合 CSS 过渡或动画实现滑动效果。适用于简单的元素入场/离场滑动。 <template>…

vue实现预览

vue实现预览

Vue实现文件预览的方法 使用Vue实现文件预览功能可以通过多种方式完成,具体取决于文件类型(图片、PDF、视频等)。以下是几种常见文件类型的实现方案。 图片预览 通过<input type…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const route…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…