当前位置:首页 > 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 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

javascript实现vue

javascript实现vue

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