当前位置:首页 > VUE

vue增删改查简单实现

2026-01-11 20:31:13VUE

Vue 增删改查简单实现

以下是一个基于 Vue 3 的简单增删改查(CRUD)实现示例,使用 Vue 的 Composition API 和响应式数据管理。

初始化项目

确保已安装 Vue 3 环境。可以通过以下命令创建一个新项目:

npm init vue@latest vue-crud-demo
cd vue-crud-demo
npm install

核心代码实现

src/App.vue 中编写以下代码:

<template>
  <div>
    <h3>用户列表</h3>
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>姓名</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="user in users" :key="user.id">
          <td>{{ user.id }}</td>
          <td>{{ user.name }}</td>
          <td>
            <button @click="editUser(user)">编辑</button>
            <button @click="deleteUser(user.id)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>

    <h3>{{ editing ? '编辑用户' : '添加用户' }}</h3>
    <input v-model="currentUser.name" placeholder="请输入姓名" />
    <button @click="saveUser">{{ editing ? '更新' : '添加' }}</button>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';

const users = reactive([
  { id: 1, name: '张三' },
  { id: 2, name: '李四' }
]);

const currentUser = reactive({
  id: null,
  name: ''
});

const editing = ref(false);

const saveUser = () => {
  if (editing.value) {
    const index = users.findIndex(u => u.id === currentUser.id);
    if (index !== -1) {
      users[index].name = currentUser.name;
    }
  } else {
    const newId = users.length > 0 ? Math.max(...users.map(u => u.id)) + 1 : 1;
    users.push({ id: newId, name: currentUser.name });
  }
  resetForm();
};

const editUser = (user) => {
  currentUser.id = user.id;
  currentUser.name = user.name;
  editing.value = true;
};

const deleteUser = (id) => {
  const index = users.findIndex(u => u.id === id);
  if (index !== -1) {
    users.splice(index, 1);
  }
};

const resetForm = () => {
  currentUser.id = null;
  currentUser.name = '';
  editing.value = false;
};
</script>

关键点说明

  • 数据管理:使用 reactive 创建响应式对象 userscurrentUserref 管理编辑状态。
  • 添加功能:通过 saveUser 方法判断当前是编辑还是添加,动态生成新 ID 并更新数组。
  • 编辑功能editUser 方法将选中用户数据填充到表单,标记为编辑状态。
  • 删除功能:通过 splice 从数组中移除指定 ID 的用户。

样式优化

可添加基础样式提升交互体验:

table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
}
button {
  margin: 0 5px;
  cursor: pointer;
}

扩展建议

  • 添加表单验证(如空值检查)。
  • 使用组件拆分逻辑(如单独的表单组件、列表组件)。
  • 集成后端 API(通过 axios 发送请求)。
  • 加入状态管理(如 Pinia)处理复杂数据流。

vue增删改查简单实现

标签: 简单vue
分享给朋友:

相关文章

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依…