一套后台管理系统的入门级的增删改查(vue3组合式api+elemment-plus)

一、页面示意:

图一

图二

二、组件结构

  • 列表组件 :index.vue,对应图一
  • 添加组件:add.vue,对应图二,用抽屉效果
  • 编辑组件:edit.vue,和添加组件的效果一个。

三、代码

1、列表组件: index.vue

<template><h1>增删改查案例</h1><div><!--查询条件  --><el-form :model="searchData" label-width="40px" :inline="true"><el-form-item label="书号"><el-input v-model="searchData.id" /></el-form-item><el-form-item label="书名"><el-input v-model="searchData.name" /></el-form-item><el-form-item label="作者"><el-input v-model="searchData.author" /></el-form-item><el-form-item label="价格"><el-input v-model="searchData.minprice" /></el-form-item><el-form-item label="-->"><el-input v-model="searchData.maxprice" /></el-form-item><el-form-item><el-button type="primary" @click="searchFn">查询</el-button><el-button @click="showAdd">添加</el-button></el-form-item></el-form></div><div><!-- 书籍列表 --><el-table :data="bookData" stripe style="width: 100%"><el-table-column prop="id" label="书号" width="180" /><el-table-column prop="name" label="书名" width="180" /><el-table-column prop="author" label="作者" /><el-table-column prop="price" label="价格" /><el-table-column  label="类型" ><template #default="scope"><span>{{typeObj[scope.row.type]}}</span></template></el-table-column><el-table-column label="图片"><template #default="scope"><img :src="scope.row.img" style="width: 100px;height: 100px" /></template></el-table-column><el-table-column fixed="right" label="操作" width="120"><template #default="scope"><el-button link type="primary" size="small" @click="showEdit(scope.row.id)">编辑</el-button><el-button link type="primary" size="small" @click.prevent="deleteBook(scope.row.id)">删除</el-button></template></el-table-column></el-table><el-drawer v-model="drawer" title="添加书籍" direction="rtl" :before-close="handleClose"><AddBook @close="closeFn" @ok="okFn"></AddBook></el-drawer><el-drawer v-model="drawerEdit" title="修改书籍" direction="rtl" ><EditBook :id="editId" @ok="editOkFn" @close="editCloseFn" ></EditBook></el-drawer></div>
</template><script  setup>
import { reactive, onMounted,ref } from "vue";
import { getBooksApi, getBooksByCondationApi, deleteBookApi } from "@/api/crud";
import AddBook from "./Add.vue";
import EditBook from "./edit.vue";const typeObj = {"new":"最新","hot":"热卖"
}// 书籍数据
let bookData = reactive([]);//一、 获取书籍信息
const getBooks = () => {getBooksApi().then(res => {if (res.data) {console.log("bookData前", bookData);// bookData = res.data;bookData.length = 0;bookData.push(...res.data);console.log("bookData后", bookData);}}).catch(err => {console.log("获取数据出错", err);})
}
onMounted(() => {getBooks();
})// 二、查询书籍
const searchData = reactive({"id": "","name": "","author": "","minprice": "","maxprice": ""
})const searchFn = () => {const temp = {};for (const key in searchData) {if (searchData[key] !== "") {temp[key] = searchData[key];}}if (temp["minprice"]) {delete temp["minprice"];temp["price_gte"] = searchData.minprice;}if (temp["maxprice"]) {delete temp["maxprice"];temp["price_lte"] = searchData.maxprice;}getBooksByCondationApi(temp).then(res => {if (res.data) {bookData.length = 0;bookData.push(...res.data);}})
}// 三、删除书籍
const deleteBook = (id) => {deleteBookApi(id).then(res => {console.log("删除的返回res", res);// 删除成功后,重新获取数据getBooks();})
}// 四、添加
const drawer = ref(false)const handleClose = (done) => {ElMessageBox.confirm('Are you sure you want to close this?').then(() => {done()}).catch(() => {// catch error})
}// 显示添加页面
const showAdd=()=>{drawer.value=true;
}const closeFn=()=>{drawer.value = false;
}const okFn=()=>{closeFn();getBooks();
}// 五、修改
const drawerEdit = ref(false);
const editId = ref("");//修改的书籍的id// 显示修改页面
const showEdit=(id)=>{editId.value=id;drawerEdit.value=true;
}const editCloseFn=()=>{editId.value ="";drawerEdit.value = false;
}const editOkFn=()=>{editCloseFn();getBooks();
}</script><style lang="css" scoped></style>

2、添加组件:add.vue

<template><el-form ref="formRef" :model="newBook" label-width="120px" :rules="rules"><el-form-item label="书号" prop="id"><el-input v-model="newBook.id" /></el-form-item><el-form-item label="书名" prop="name"><el-input v-model="newBook.name" /></el-form-item><el-form-item label="作者" prop="author"><el-input v-model="newBook.author" /></el-form-item><el-form-item label="价格" prop="price"><el-input v-model="newBook.price" /></el-form-item><el-form-item label="图片" prop="img"><el-input v-model="newBook.img" /></el-form-item><el-form-item label="类型" prop="type"><!-- <el-input v-model="newBook.type" /> --><el-select v-model="newBook.type" class="m-2" placeholder="请选择书籍类型" size="large"><!-- <el-option v-for="item in booktypes" :key="item.id" :label="item.name" :value="item.value" /> --><el-option label="最新" value="new" /><el-option label="热卖" value="hot" /></el-select></el-form-item><el-form-item><el-button type="primary" @click="addBookFn(formRef)">添加</el-button><el-button @click="cancelFn">取消</el-button></el-form-item></el-form>
</template><script  setup>
import { reactive, ref } from 'vue';
import { addBookApi,getBookTypeApi } from "@/api/crud"const emit = defineEmits(['close', "ok"]);const booktypes = reactive([]);const getBookType = () => {getBookTypeApi().then(res=>{if(res.data){booktypes.length=0;booktypes.push(...res.data);}})
}getBookType();const formRef = ref();
//  定义添加的书籍
const newBook = reactive({"id": "","name": "","author": "","price": 0,"img": "","type": ""
})// 表单验证的规则
const rules = reactive({"id": [{ required: true, message: '请输入书号', trigger: 'blur' }],"name": [{ required: true, message: '请输入书名', trigger: 'blur' }],"author": [{ required: true, message: '请输入作者', trigger: 'blur' }],"price": [{ required: true, message: '请输入价格', trigger: 'blur' }],"img": [{ required: true, message: '请输入图片地址', trigger: 'blur' }],"type": [{ required: true, message: '请输入类型', trigger: 'blur' }],
})const addBookFn = (formEl) => {if (!formEl) {return;}console.log("newBook",newBook);formEl.validate((isOk) => {if (isOk) {addBookApi(newBook).then(res => {console.log(res);if (res.data) {// 添加成功alert("添加成功!");// 关闭弹窗emit("ok");}}).catch(err => {})}})}const cancelFn = () => {emit('close');
}</script>

3、编辑组件:edit.vue

<template><el-form ref="formRef" :model="editBook" label-width="120px" :rules="rules"><el-form-item label="书号" prop="id"><el-input v-model="editBook.id" /></el-form-item><el-form-item label="书名" prop="name"><el-input v-model="editBook.name" /></el-form-item><el-form-item label="作者" prop="author"><el-input v-model="editBook.author" /></el-form-item><el-form-item label="价格" prop="price"><el-input v-model="editBook.price" /></el-form-item><el-form-item label="图片" prop="img"><el-input v-model="editBook.img" /></el-form-item><el-form-item label="类型" prop="type"><el-input v-model="editBook.type" /></el-form-item><el-form-item><el-button type="primary" @click="editBookFn(formRef)">修改</el-button><el-button @click="cancelFn">取消</el-button></el-form-item></el-form>
</template><script  setup>
import { reactive, ref,watch } from 'vue';
import { getBookDetailApi,editBookApi } from '@/api/crud';const props = defineProps(["id"]);const formRef = ref();//  定义修改的书籍
const editBook = reactive({"id": "","name": "","author": "","price": 0,"img": "","type": ""
})// 获取当前修改的书籍信息
const getBookDetail = async () => {try {const res = await getBookDetailApi(props.id);// editBook = res.data;for(let key in editBook){editBook[key] = res.data[key];}} catch (err) {console.log("服务器出错",err);}
}console.log("props",props);watch(props,()=>{console.log("props.id变了");getBookDetail();
},{deep:true,immediate:true})const emit = defineEmits(['close', "ok"]);// 表单验证的规则
const rules = reactive({"id": [{ required: true, message: '请输入书号', trigger: 'blur' }],"name": [{ required: true, message: '请输入书名', trigger: 'blur' }],"author": [{ required: true, message: '请输入作者', trigger: 'blur' }],"price": [{ required: true, message: '请输入价格', trigger: 'blur' }],"img": [{ required: true, message: '请输入图片地址', trigger: 'blur' }],"type": [{ required: true, message: '请输入类型', trigger: 'blur' }],
})const editBookFn = (formEl) => {if (!formEl) {return;}formEl.validate((isOk) => {if (isOk) {editBookApi(props.id,editBook).then(res => {console.log(res);if (res.data) {alert("修改成功!");// 关闭弹窗emit("ok");}}).catch(err => {})}})}const cancelFn = () => {emit('close');
}</script>

4、补充【api和axios封装】:api/crud.js;utils/serviceMock.js

api/crud.js

import service from "@/utils/serviceMock.js";// 获取所有书籍
export const getBooksApi = ()=>service.get('/books');// 根据编号获取书籍详情
export const getBookDetailApi = (id)=>service.get('/books/'+id);// 查询书籍
export const getBooksByCondationApi = (params)=>service.get('/books',{params});// 删除书籍
export const deleteBookApi = (id)=>service.delete('/books/'+id);// 添加书籍
export const addBookApi = (data)=>service.post('/books',data);// 修改书籍
export const editBookApi = (id,data)=>service.put('/books/'+id,data);// 获取书籍类型
export const getBookTypeApi = ()=>service.get('/bookType');
utils/serviceMock.js
import axios from 'axios';
import store from '@/store';const service = axios.create({baseURL:"http://localhost:3000"
})// 请求拦截器:所有请求的公共业务
service.interceptors.request.use(config=>{store.commit("showLoading");return config;
})// 响应拦截器
service.interceptors.response.use((res)=>{store.commit("hideLoading");// loading.close();return res;
})export default service;

5、mock数据,用json-server

{"books": [{"id": "878912","name": "水浒1","author": "施耐庵","price": 51.5,"img": "/src/assets/imgs/01.jpg","type": "hot"},{"id": "878913","name": "红楼梦","author": "曹雪芹2","price": 51.8,"img": "/src/assets/imgs/01.jpg","type": "hot"},{"id": "878917","name": "论语1","author": "王锐1","price": "5.38","img": "/imgs/img3.jpg","type": "new"},{"id": "878918","name": "老子","author": "李家恒","price": 54.8,"img": "/imgs/img4.jpg","type": "new"},{"id": "878919","name": "孟子2","author": "李家恒","price": 54.8,"img": "/images/img4.jpg","type": "new"},{"id": "878920","name": "孟子3","author": "李家恒","price": 54.8,"img": "/images/img4.jpg","type": "new"},{"id": "878921","name": "孟子4","author": "李家恒","price": 54.8,"img": "/images/img4.jpg","type": "new"},{"id": "878922","name": "孟子5","author": "李家恒","price": 54.8,"img": "/images/img4.jpg","type": "new"},{"id": "878923","name": "孟子6","author": "李家恒","price": 54.8,"img": "/images/img4.jpg","type": "new"},{"id": "01008","name": "霸道总裁爱上我","author": "曹宇","price": "9.9","img": "/src/imgs/01.jpg","type": "hot"},{"id": "01009","name": "西厢记","author": "赵蕊","price": "10.9","img": "/src/assets/imgs/01.jpg","type": "hot"},{"id": "01010","name": "钢铁是怎样炼成的","author": "奥斯特洛夫斯基","price": "11.9","img": "/src/assets/imgs/02.jpg","type": "hot"},{"id": "01011","name": "12","author": "22","price": 0,"img": "1","type": "hot"},{"id": "01012","name": "假如我是亿万富翁","author": "金莉","price": "1000","img": "/src/assets/imgs/01.jpg","type": "new"}],
}                         

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/184305.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我在Vscode学OpenCV 图像处理一(阈值处理、形态学操作【连通性,腐蚀和膨胀,开闭运算,礼帽和黑帽,内核】)

文章目录 一、阈值处理1.1 OpenCV 提供了函数 cv2.threshold()和函数 cv2.adaptiveThreshold()&#xff0c;用于实现阈值处理1.1.1. cv2.threshold()&#xff1a;(1)在函数cv2.threshold()中&#xff0c;参数threshold_type用于指定阈值处理的方式。它有以下几种可选的阈值类型…

哈希桶——开放定址法

哈希表的迭代器&#xff1a; 迭代器模板介绍&#xff1a; template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash> K:关键词类型 T:存储的数据类型 Ref:T& &#xff08;operator*() 解引用函数的返回类型&#xff09; Ptr:T* (oper…

CDA level-2 备考经验分享 转数据分析师CDA证书备考 考试相关说明

抓住了23年的尾巴&#xff0c;正好给我考过了CDA level-2 &#xff0c;虽然今年只有这几个小收获&#xff0c;但是还是很开心了&#xff0c;毕竟知足常乐嘛。 由于工作原因&#xff0c;因此复习都是间断性的&#xff0c;勉勉强强给通过了&#xff0c;只得了个C。 考试注册报名与…

台灯到底对眼睛好不好?推荐高品质的护眼台灯

其实只要我们挑选一盏专业的台灯&#xff0c;并且正确的使用&#xff0c;那么台灯对眼睛是有很大的好处的&#xff01;如今夜间工作、学习已然成为了再常见不过的事情&#xff0c;在夜间最大的痛点就是光照不足&#xff0c;如果单靠室内灯是远远不足的&#xff0c;而且光线的分…

基于Springboot的房产销售系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的房产销售系统(有报告)。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring Sp…

次世代建模纹理贴图怎么做?

在线工具推荐&#xff1a; 三维数字孪生场景工具 - GLTF/GLB在线编辑器 - Three.js AI自动纹理化开发 - YOLO 虚幻合成数据生成器 - 3D模型在线转换 - 3D模型预览图生成服务 1、什么是次时代建模&#xff1f; "次世代建模"是一个术语&#xff0c;通常用来描述…

Linux--2.6内核调度和环境变量

&#x1f4d8;北尘_&#xff1a;个人主页 &#x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上&#xff0c;不忘来时的初心 文章目录 一、Linxu2.6内核进程调度队列1、一个CPU拥有一个runqueue2、优先级3、优先级活动队列4、过期…

VC++调试QT源码

环境&#xff1a;vs2017 qt 5.14.2 1&#xff1a;首先我们需要选择我们的源码路径 右键解决方案-》属性-》通用属性-》调试源文件-》在窗口内添加QT下载时的源码**.src文件夹**&#xff0c;这里最好把源码 D:\software\QT\path\5.14.2\Src 源文件里面的Src文件做一个备份出来…

Android UiAutoMatorViewer打不开

UIAutoMatorViewer是个很好用的工具&#xff0c;能解析出任意手机页面的UI树&#xff0c;非常方便。 工具位置&#xff1a;SDK\tools\bin\uiautomatorviewer.bat 一般双击就能打开。 但有时会打不开&#xff0c;双击后无反应&#xff0c;在cmd窗口中运行也是如此。 这种情况…

【Linux】第二十三站:缓冲区

文章目录 一、一些奇怪的现象二、用户级缓冲区三、用户级缓冲区刷新问题四、一些其他问题1.缓冲区刷新的时机2.为什么要有这个缓冲区3.这个缓冲区在哪里&#xff1f;4.这个FILE对象属于用户呢&#xff1f;还是操作系统呢&#xff1f;这个缓冲区&#xff0c;是不是用户级的缓冲区…

Python基础语法之学习占位符

Python基础语法之学习占位符 一、代码二、效果 一、代码 name "张三" sex "男" age 10 money 12.5# 通过占位符完成拼接 print("姓名&#xff1a;%s" % name) print("姓名&#xff1a;%s,性别&#xff1a;%s" % (name, sex))text…

6.16二叉搜索树中的搜索(LC700-E)

算法&#xff1a; 二叉搜索树自带顺序&#xff0c;所以不用强调前、中、后序。 调试过程&#xff1a; 原因&#xff1a;初始化变量result时&#xff0c;没有给result赋值 正确代码&#xff1a; /*** Definition for a binary tree node.* public class TreeNode {* int…

『C++成长记』构造函数和析构函数

&#x1f525;博客主页&#xff1a;小王又困了 &#x1f4da;系列专栏&#xff1a;C &#x1f31f;人之为学&#xff0c;不日近则日退 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 目录 一、类的六个个默认成员函数 &#x1f4d2;1.1认识默认成员函数 二、构造函数 …

VsCode中使用功能vite创建vue3+js项目报错

VsCode中使用功能vite创建vue3js项目报错 VsCode中使用功能vite创建vue3js项目import模块报错如下处理方法 VsCode中使用功能vite创建vue3js项目import模块报错如下 处理方法 在项目根目录新建jsconfig.json {"compilerOptions": {"baseUrl": "./&q…

vscode非常好用的扩展插件

1、Code Spell Checker&#xff1a; 帮助我们检查单词是否拼写错误&#xff0c;检查规则遵循驼峰拼写法。 2、Color Highlight&#xff1a;高亮显示颜色值 3、Svg Preview&#xff1a; 实时预览svg图片&#xff08;修改width、height、fill等值来实时查看效果&#xff09; 4、…

Kong处理web服务跨域

前言 好久没写文章了&#xff0c;大概有半年多了&#xff0c;这半年故事太多&#xff0c;本文写不下&#xff0c;就写写文章标题问题&#xff01; 问题描述 关于跨域的本质问题我这里不过多介绍&#xff0c;详细请看历史文章 跨域产生的原因以及常见的解决方案。 我这边是新…

PyCharm免费安装和新手使用教程

PyCharm是一款由JetBrains公司开发的Python集成开发环境&#xff08;IDE&#xff09;。它提供了一系列强大的功能&#xff0c;包括自动代码完成、语法高亮、自动缩进、代码重构、调试器、测试工具、版本控制工具等&#xff0c;使开发者可以更加高效地开发Python应用程序。 新手…

主流数据库类型总结

前言&#xff1a;随着互联网的高速发展&#xff0c;为了满足不同的应用场景&#xff0c;数据库的种类越来越多容易混淆&#xff0c;所以有必要在此总结一下。数据库根据数据结构可分为关系型数据库和非关系型数据库。非关系型数据库中根据应用场景又可分为键值&#xff08;Key-…

Leetcode394. 字符串解码

Every day a Leetcode 题目来源&#xff1a;394. 字符串解码 解法1&#xff1a;栈 本题中可能出现括号嵌套的情况&#xff0c;比如 2[a2[bc]]&#xff0c;这种情况下我们可以先转化成 2[abcbc]&#xff0c;在转化成 abcbcabcbc。我们可以把字母、数字和括号看成是独立的 TO…

滴滴就系统故障再次致歉

滴滴出行官博发文就11月27日夜间发生的系统故障再次致歉&#xff0c;同时表示&#xff0c;初步确定&#xff0c;这起事故的起因是底层系统软件发生故障&#xff0c;并非网传的“遭受攻击”&#xff0c;后续将深入开展技术风险隐患排查和升级工作&#xff0c;全面保障服务稳定性…