当前位置:首页 > 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 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 get…