当前位置:首页 > 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,模拟后端数据:

vue实现crud功能

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

vue实现crud功能

<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调用:

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 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…