当前位置:首页 > VUE

vue实现crud功能

2026-02-20 09:56:09VUE

Vue实现CRUD功能

安装Vue及相关依赖

确保已安装Vue CLI,通过以下命令创建项目:

npm install -g @vue/cli
vue create vue-crud-demo
cd vue-crud-demo

安装axios用于HTTP请求:

npm install axios

创建数据模型

src目录下创建data.js,模拟后端数据:

export const items = [
  { id: 1, name: 'Item 1', description: 'Description 1' },
  { id: 2, name: 'Item 2', description: 'Description 2' }
];

实现CRUD组件

创建CrudComponent.vue文件:

<template>
  <div>
    <h3>Item List</h3>
    <table>
      <tr v-for="item in items" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.description }}</td>
        <td>
          <button @click="editItem(item)">Edit</button>
          <button @click="deleteItem(item.id)">Delete</button>
        </td>
      </tr>
    </table>

    <h3>{{ editing ? 'Edit Item' : 'Add Item' }}</h3>
    <input v-model="currentItem.name" placeholder="Name">
    <input v-model="currentItem.description" placeholder="Description">
    <button @click="saveItem">{{ editing ? 'Update' : 'Save' }}</button>
  </div>
</template>

<script>
import { items } from '../data';

export default {
  data() {
    return {
      items: [...items],
      currentItem: { id: null, name: '', description: '' },
      editing: false
    };
  },
  methods: {
    saveItem() {
      if (this.editing) {
        const index = this.items.findIndex(i => i.id === this.currentItem.id);
        this.items.splice(index, 1, { ...this.currentItem });
      } else {
        this.currentItem.id = Math.max(...this.items.map(i => i.id)) + 1;
        this.items.push({ ...this.currentItem });
      }
      this.resetForm();
    },
    editItem(item) {
      this.currentItem = { ...item };
      this.editing = true;
    },
    deleteItem(id) {
      this.items = this.items.filter(item => item.id !== id);
    },
    resetForm() {
      this.currentItem = { id: null, name: '', description: '' };
      this.editing = false;
    }
  }
};
</script>

集成到主应用

修改App.vue

<template>
  <div id="app">
    <CrudComponent />
  </div>
</template>

<script>
import CrudComponent from './components/CrudComponent.vue';

export default {
  components: { CrudComponent }
};
</script>

运行项目

启动开发服务器:

npm run serve

连接真实后端(可选)

替换CrudComponent.vue中的方法为API调用:

vue实现crud功能

methods: {
  async fetchItems() {
    const response = await axios.get('/api/items');
    this.items = response.data;
  },
  async saveItem() {
    if (this.editing) {
      await axios.put(`/api/items/${this.currentItem.id}`, this.currentItem);
    } else {
      await axios.post('/api/items', this.currentItem);
    }
    this.fetchItems();
    this.resetForm();
  },
  async deleteItem(id) {
    await axios.delete(`/api/items/${id}`);
    this.fetchItems();
  }
},
created() {
  this.fetchItems();
}

关键点说明

  • 使用v-model实现表单双向绑定
  • 通过条件渲染区分创建和编辑模式
  • 数组方法如filterfindIndex处理数据操作
  • 对象展开运算符...保持数据不可变性

以上实现可根据实际需求扩展验证、分页等功能。

标签: 功能vue
分享给朋友:

相关文章

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…