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

删除功能实现

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

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

编辑功能实现

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

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

更新功能实现

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

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:

vue里实现增删改查

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实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适合…