当前位置:首页 > VUE

vue增删改查简单实现

2026-02-09 09:11:59VUE

实现Vue增删改查功能

使用Vue 3的Composition API配合Element Plus组件库实现基础CRUD功能。

安装依赖

vue增删改查简单实现

npm install vue@next element-plus axios

基础组件结构

<template>
  <div class="crud-container">
    <el-button type="primary" @click="handleAdd">新增</el-button>

    <el-table :data="tableData" border>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="formData" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="formData.age" type="number" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

核心逻辑实现

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

const tableData = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)
const currentId = ref(null)

const formData = reactive({
  name: '',
  age: ''
})

// 获取数据
const fetchData = async () => {
  const res = await axios.get('/api/users')
  tableData.value = res.data
}

// 新增操作
const handleAdd = () => {
  dialogTitle.value = '新增用户'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

// 编辑操作
const handleEdit = (row) => {
  dialogTitle.value = '编辑用户'
  isEdit.value = true
  currentId.value = row.id
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 删除操作
const handleDelete = async (row) => {
  await axios.delete(`/api/users/${row.id}`)
  fetchData()
}

// 提交表单
const submitForm = async () => {
  if (isEdit.value) {
    await axios.put(`/api/users/${currentId.value}`, formData)
  } else {
    await axios.post('/api/users', formData)
  }
  dialogVisible.value = false
  fetchData()
}

// 重置表单
const resetForm = () => {
  formData.name = ''
  formData.age = ''
}

onMounted(() => {
  fetchData()
})
</script>

样式优化

<style scoped>
.crud-container {
  padding: 20px;
}
.el-table {
  margin-top: 20px;
}
</style>

后端API示例(Node.js Express)

// 模拟数据存储
let users = [
  { id: 1, name: '张三', age: 25 },
  { id: 2, name: '李四', age: 30 }
]

// 获取所有用户
app.get('/api/users', (req, res) => {
  res.json(users)
})

// 新增用户
app.post('/api/users', (req, res) => {
  const newUser = {
    id: users.length + 1,
    ...req.body
  }
  users.push(newUser)
  res.json(newUser)
})

// 更新用户
app.put('/api/users/:id', (req, res) => {
  const id = parseInt(req.params.id)
  const index = users.findIndex(user => user.id === id)
  if (index !== -1) {
    users[index] = { ...users[index], ...req.body }
    res.json(users[index])
  } else {
    res.status(404).send('用户不存在')
  }
})

// 删除用户
app.delete('/api/users/:id', (req, res) => {
  const id = parseInt(req.params.id)
  users = users.filter(user => user.id !== id)
  res.sendStatus(204)
})

关键点说明

  1. 使用refreactive管理组件状态
  2. 通过axios进行HTTP请求交互
  3. 利用Element Plus的el-tableel-dialog组件构建UI
  4. 区分新增和编辑状态,复用同一个表单对话框
  5. 每次操作后重新获取数据保持同步

可根据实际需求扩展功能,如添加表单验证、分页查询、批量操作等。

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

相关文章

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…