封装element-plus上传图片组件

简介: 封装element-plus上传图片组件
<template>
    <el-upload v-model:file-list="upload.fileList" :action="upload.action" list-type="picture-card"
        :before-upload="upload.before" :on-success="upload.success" limit :on-preview="upload.proview"
        :data-fileListCount="upload.fileList.length" :name="upload.name" :on-remove="upload.remove">
        <el-icon>
            <Plus />
        </el-icon>
    </el-upload>
    <el-dialog v-model="upload.dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false"
        append-to-body>
        <img w-full :src="upload.dialogImageUrl" alt="" style="width:100%;height:100%" />
    </el-dialog>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import baseUrl from '@/api/baseUrl.js'
const props = defineProps({
    imageUrl: String,
});
const emit = defineEmits(['getImageUrl'])
const upload = reactive({
    name: 'file',
    action: '接口地址',
    fileList: props.imageUrl ? [{ url: props.imageUrl }] : [],
    dialogImageUrl: '',
    dialogVisible: false,
    before: (file) => {
        const type = ['image/png', 'image/jpeg', 'image/gif'];
        if (!type.includes(file.type)) {
            ElMessage.error('不支持该类型文件')
            return false
        }
    },
    proview: ({ url }) => {
        console.log(url);
        upload.dialogImageUrl = url;
        upload.dialogVisible = true;
    },
    success: (res, uploadFile, uploadFiles) => {
        console.log(res);
        console.log(uploadFile);
        if (res.url && res.code === 1) {
            upload.fileList = [{
                url: uploadFile.url,
            }];
            emit('getImageUrl', res.url, uploadFile.url);
            // document.querySelector('.el-upload--picture-card').style.display = "none"
        } else {
            ElMessage({ type: 'warning', message: res.msg });
            upload.fileList = [];
            emit('getImageUrl', '', '');
        }
    },
    remove: (file, files) => {
        upload.fileList = [];
        emit('getImageUrl', '', '');
    }
})
</script>
<style scope lang="scss">
[data-fileListCount='1'] {
    .el-upload--picture-card {
        display: none !important;
    }
}
</style>

页面使用:

import UploadAvatar from '@/components/UploadAvatar/index.vue';

模版区使用

<UploadAvatar @getImageUrl="getImageUrl" :imageUrl="table.image" />
相关文章
红警源代码居然开源了....
红警源代码居然开源了....
507 0
|
Android开发
autojs修改悬浮窗按钮点击事件
牙叔教程 简单易懂
1670 0
|
5月前
|
运维 数据挖掘
raid5数据恢复—5盘RAID5阵列重建为4盘RAID5阵列后的数据恢复案例
一台服务器上有一组raid5磁盘阵列,该raid5阵列有5块硬盘。服务器数据无备份。 服务器一块硬盘掉线,运维人员在没有完全了解服务器的具体情况下,使用服务器上的另外4块正常硬盘重建了一组新的raid5阵列,导致服务器原有数据丢失。
|
9月前
|
Swift iOS开发 开发者
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
1383 13
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
|
JavaScript 前端开发 数据可视化
Element Plus图片上传组件二次扩展
Element Plus图片上传组件二次扩展
504 0
|
人工智能 弹性计算 自动驾驶
2023 AI开发者生态报告:技术生态、开发范式与应用案例全景
随着人工智能技术的飞速发展,全球IT市场对AI的投入持续增长,预计到2027年将达到4236亿美元。
|
弹性计算 物联网 Serverless
如何获得ICP备案服务码?
如何获得ICP备案服务码?
|
对象存储
阿里云oss-cloud-sdk-springboot3兼容问题
阿里云oss-cloud-sdk-springboot3兼容问题
489 0
|
Oracle 安全 Java
最新Java JDK 21:全面解析与新特性探讨
最新Java JDK 21:全面解析与新特性探讨
940 0
最新Java JDK 21:全面解析与新特性探讨
封装element-plus上传图片组件
v封装element-plus上传图片组件