渲染你刚刚上传的图片,再进行二次上传

封装里面的内容

写了刚刚的路径

这是封装的组件代码
<template>
    <div>
        <el-upload :action="actionUrl" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :before-upload="beforeAvatarUpload" :on-success="handleAvatarScucess" :on-error="handleAvatarError" :file-list="imageUrl">
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog v-model="dialogVisible" size="small">
            <img width="100%" :src="dialogImageUrl" alt="获取图片失败">
        </el-dialog>
    </div>
</template>

<script>
/**
 * 多图片上传组件(最多5张)
 * 上传成功和删除图片都出发自定义事件,向父组件传递新的图片数据
 */
// import { basicConfig } from '@/config/config.js';
// import '@/static/style/fengModule/imageUpload.scss';

export default {
	props: {
        imageUrl: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            dialogVisible: false,
            dialogImageUrl: [],
            actionUrl:'http://upload.binguobox.com:8080/api/fileserver/upload/pub?a=1'
        }
    },
    methods: {
    	showDialog(val) {
            this.itemImageUrl = val.url;
            this.dialogVisible = true;
       },
        handleRemove(file, fileList) {
            // 移除图片钩子
            let imageListUrl = []
            for (let item of fileList) {
                imageListUrl.push(item.name)
            }
            this.$emit('increment', imageListUrl)
        },
        handlePictureCardPreview(file) {
            // 查看大图
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        beforeAvatarUpload(file) {
            // 上传文件前的钩子
            const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png')
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isJPG) {
              this.$message.error('上传的图片只能是 JPG/PNG 格式!')
            }
            if (!isLt2M) {
              this.$message.error('上传的图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        },
        handleAvatarScucess(response, file, fileList) {
            // 图片上传成功钩子,使用自定义事件给父组件传数据
            if(response.status === 404) {
                this.$alert(response.data, '系统通知', { confirmButtonText: '确定', type: 'error' })
                return false
            }
            let imageListUrl = [];
            for (let item of fileList) {
                if(item.response){
                   imageListUrl.push(item.response.data);
                }else{
                   imageListUrl.push(item.name); 
                }
              
            }
            this.$emit('increment', imageListUrl)
        },
        handleAvatarError(err, file) {
            // 图片上传失败钩子
            this.$notify({ title: '系统通知', message: '图片上传失败,请确认图片格式大小正确后重试', type: 'error' })
        }
    }
}

</script>

页面中的使用



posted @   李美玲  阅读(1270)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 在 VS Code 中,一键安装 MCP Server!
· 千万级大表的优化技巧
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
点击右上角即可分享
微信分享提示