当前位置:首页 > 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 方法实现数据添加:

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

删除功能

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

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 请求:

vue里实现增删改查

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实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue实现分步

vue实现分步

Vue 实现分步功能 在 Vue 中实现分步功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 通过动态组件切换不同的步骤内容,结合 v-if 或 component 标签实现分步逻辑。…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…

vue实现hexo

vue实现hexo

Vue 集成 Hexo 的实现方法 Hexo 是一个静态博客框架,而 Vue 是一个前端框架。将 Vue 集成到 Hexo 中可以通过以下方式实现: 在 Hexo 中使用 Vue 组件 通过 Hex…