后台管理的图片上传功能(组件方法),最近写到后台管理时需要用到组件上传图片(为了美观)
组件上传功能
action属性是要写入你的接口的上传图片,不然你是上传空白的 (切记这里是必填的)
这里呢就是具体代码
<template> <div> <p>商品图片:</p> <div class="commodity_img"> <!-- 上传图片 --> <el-upload list-type="picture-card" :action="'/pcapi/File/fileimg'" :on-change="handleChange" :before-remove="beforeRemove" :on-preview="handlePictureCardPreview" :file-list="fileList" multiple limit="1" name="img" > <el-icon class="avatar-uploader-icon"> <Plus /> </el-icon> </el-upload> </div> </div> </template> <script setup> import { Plus } from "@element-plus/icons-vue"; import { ElMessageBox } from "element-plus"; // 上传图片 const fileList = ref([]); //上传文件 const handleChange = (file: { status; response: { front_file }; }) => { if (file.status == "success") { fileList.value.push(file.response.front_file ); } // console.log(fileList.value); //完成后可以在添加或编辑上写入fileList.value.join(',')即可 }; // 删除 const beforeRemove = () => { const result = new Promise((resolve, reject) => { ElMessageBox.confirm("此操作将删除该图片, 是否继续?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" }) .then(() => { resolve(); }) .catch(() => { reject(false); }); }); return result; }; </script>