当前位置:首页 > VUE

vue里实现增删改查

2026-02-09 20:13:43VUE

在 Vue 中实现增删改查(CRUD)

数据绑定与列表渲染

使用 v-for 指令渲染数据列表,结合 v-model 实现表单双向绑定。示例代码:

<template>
  <div>
    <input v-model="newItem.name" placeholder="名称" />
    <button @click="addItem">添加</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }}
        <button @click="editItem(index)">编辑</button>
        <button @click="deleteItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

添加功能

通过数组的 push 方法实现数据添加:

vue里实现增删改查

data() {
  return {
    items: [],
    newItem: { name: '' }
  }
},
methods: {
  addItem() {
    if (this.newItem.name.trim()) {
      this.items.push({...this.newItem});
      this.newItem.name = '';
    }
  }
}

删除功能

使用 splice 方法根据索引删除数据:

vue里实现增删改查

methods: {
  deleteItem(index) {
    this.items.splice(index, 1);
  }
}

编辑功能

通过临时存储编辑状态实现修改:

data() {
  return {
    editingIndex: -1,
    tempItem: {}
  }
},
methods: {
  editItem(index) {
    this.editingIndex = index;
    this.tempItem = {...this.items[index]};
  },
  saveEdit() {
    Object.assign(this.items[this.editingIndex], this.tempItem);
    this.cancelEdit();
  },
  cancelEdit() {
    this.editingIndex = -1;
  }
}

与后端交互

使用 axios 发送 HTTP 请求:

import axios from 'axios';

methods: {
  async fetchData() {
    const res = await axios.get('/api/items');
    this.items = res.data;
  },
  async saveItem(item) {
    await axios.post('/api/items', item);
  }
}

完整组件示例

<template>
  <div>
    <input v-model="tempItem.name" />
    <button v-if="editingIndex === -1" @click="addItem">添加</button>
    <template v-else>
      <button @click="saveEdit">保存</button>
      <button @click="cancelEdit">取消</button>
    </template>

    <ul>
      <li v-for="(item, index) in items" :key="item.id">
        {{ item.name }}
        <button @click="editItem(index)">编辑</button>
        <button @click="deleteItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      tempItem: { name: '' },
      editingIndex: -1
    }
  },
  methods: {
    addItem() {
      this.items.push({...this.tempItem});
      this.tempItem.name = '';
    },
    editItem(index) {
      this.editingIndex = index;
      this.tempItem = {...this.items[index]};
    },
    saveEdit() {
      this.items[this.editingIndex] = {...this.tempItem};
      this.cancelEdit();
    },
    deleteItem(index) {
      this.items.splice(index, 1);
    },
    cancelEdit() {
      this.editingIndex = -1;
      this.tempItem.name = '';
    }
  }
}
</script>

标签: vue
分享给朋友:

相关文章

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue实现音乐

vue实现音乐

Vue实现音乐播放功能 使用Vue实现音乐播放功能可以通过多种方式完成,以下是几种常见的实现方法: 使用HTML5 Audio API 在Vue组件中直接使用HTML5的Audio对象来控制音乐播放…

vue实现cs程序

vue实现cs程序

Vue 实现 CS(客户端-服务器)程序 Vue 本身是一个前端框架,主要用于构建用户界面。要实现一个完整的 CS(客户端-服务器)程序,需要结合后端技术。以下是实现的基本思路和步骤: 前端部分(V…

vue实现点击提交

vue实现点击提交

Vue 实现点击提交功能 在 Vue 中实现点击提交功能通常涉及表单处理和事件绑定。以下是几种常见的实现方式: 使用 v-on 或 @ 绑定点击事件 通过 v-on:click 或简写 @click…

vue缺省页实现

vue缺省页实现

Vue 缺省页实现方法 使用条件渲染控制显示 通过v-if或v-show指令控制缺省页的显示。当数据为空时展示缺省组件,否则显示正常内容。 <template> <div>…