一套后台管理系统的入门级的增删改查(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.hqwc.cn/news/231734.html

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

相关文章

代码随想录算法训练营第五十九天| 503.下一个更大元素II 42. 接雨水

文档讲解&#xff1a;代码随想录 视频讲解&#xff1a;代码随想录B站账号 状态&#xff1a;看了视频题解和文章解析后做出来了 503.下一个更大元素II class Solution:def nextGreaterElements(self, nums: List[int]) -> List[int]:res [-1] * len(nums)stack []for i in…

06 # 枚举类型

一个角色判断例子 function initByRole(role) {if (role 1 || role 2) {// do sth} else if (role 3 || role 4) {// do sth} else if (role 5) {// do sth} else {// do sth} }上面的代码存在的问题&#xff1a; 可读性差&#xff1a;很难记住数字的含义可维护性差&…

基于单片机智能液位水位监测控制系统

**单片机设计介绍&#xff0c; 基于单片机智能液位水位监测控制系统 文章目录 一 概要特点应用场景工作原理实现方式 系统功能实时监测控制调节报警功能数据记录与分析 总结 二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 ## 系统介绍 基于单片机…

私域数字化建设:解锁企业融资新引擎

私域数字化建设对于增加企业融资能力的机遇是十分重要的&#xff0c;随着数字化经济的快速发展和数据技术的不断进步&#xff0c;企业需要正确认识到数据资产的重要性和私域数字化建设在提升融资能力等方面所带来的机遇。 近期&#xff0c;财政部发布了《企业数据资源相关会计…

CONTROLLING VISION-LANGUAGE MODELS FOR MULTI-TASK IMAGE RESTORATION

CONTROLLING VISION-LANGUAGE MODELS FOR MULTI-TASK IMAGE RESTORATION (Paper reading) Ziwei Luo, Uppsala University, ICLR under review(6663), Cited:None, Stars: 350, Code, Paper. 1. 前言 像CLIP这样的视觉语言模型已经显示出对零样本或无标签预测的各种下游任务…

深度学习毕设项目 深度学习 python opencv 动物识别与检测

文章目录 0 前言1 深度学习实现动物识别与检测2 卷积神经网络2.1卷积层2.2 池化层2.3 激活函数2.4 全连接层2.5 使用tensorflow中keras模块实现卷积神经网络 3 YOLOV53.1 网络架构图3.2 输入端3.3 基准网络3.4 Neck网络3.5 Head输出层 4 数据集准备4.1 数据标注简介4.2 数据保存…

F. Magic Will Save the World

首先积攒了能量打了怪再积攒是没有意义的&#xff0c;可以直接积攒好&#xff0c;然后一次性进行攻击 那么怎么进行攻击了&#xff1f;可以尽量的多选怪物使用水魔法攻击剩余的再用火魔法进行攻击&#xff0c; 也就是只要存在合法的体积&#xff08;即装入背包的怪物的体积之…

geoserver根据数据字段动态设置样式

一、数据展示&#xff1a; 二、样式设置 <?xml version"1.0" encoding"UTF-8"?> <StyledLayerDescriptor version"1.0.0" xsi:schemaLocation"http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns"http://…

Linux下Docker 离线安装详细步骤,亲测成功

1.离线原因&#xff1a;公司新创不能使用开元linux&#xff0c;使用了一个变种centOS&#xff0c;致使yum被禁 2.步骤&#xff1a; 2.1 下载docker tar包&#xff0c;下载地址&#xff1a;Index of linux/https://download.docker.com/linux/ 2.2 新建自己的软件目录&am…

智能AI问答系统ChatGPT网站系统源码+Midjourney绘画+支持GPT-4-Turbo模型+支持GPT-4图片理解能力

一、AI创作系统 SparkAi创作系统是基于ChatGPT进行开发的Ai智能问答系统和Midjourney绘画系统&#xff0c;支持OpenAI-GPT全模型国内AI全模型。本期针对源码系统整体测试下来非常完美&#xff0c;可以说SparkAi是目前国内一款的ChatGPT对接OpenAI软件系统。那么如何搭建部署AI…

解决Unable to preventDefault inside passive event listener invocation.报错

报错信息&#xff1a; 这个报错大致说的是&#xff1a;无法在被动事件侦听器调用中防止Default 查了其他博主的解决办法&#xff1a;比如&#xff1a; 1、声明事件监听的时候设置为主动事件监听&#xff1a; window.addEventListener(‘touchmove’, handler, { passive: fal…

基于傅里叶变换的运动模糊图像恢复算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1、傅里叶变换与图像恢复 4.2、基于傅里叶变换的运动模糊图像恢复算法原理 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 %获取角度 img…