• 封装element-plus上传图片组件


    1. <template>
    2. <el-upload v-model:file-list="upload.fileList" :action="upload.action" list-type="picture-card"
    3. :before-upload="upload.before" :on-success="upload.success" limit :on-preview="upload.proview"
    4. :data-fileListCount="upload.fileList.length" :name="upload.name" :on-remove="upload.remove">
    5. <el-icon>
    6. <Plus />
    7. el-icon>
    8. el-upload>
    9. <el-dialog v-model="upload.dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false"
    10. append-to-body>
    11. <img w-full :src="upload.dialogImageUrl" alt="" style="width:100%;height:100%" />
    12. el-dialog>
    13. template>
    14. <script setup>
    15. import { ref, reactive } from 'vue'
    16. import { Plus } from '@element-plus/icons-vue'
    17. import baseUrl from '@/api/baseUrl.js'
    18. const props = defineProps({
    19. imageUrl: String,
    20. });
    21. const emit = defineEmits(['getImageUrl'])
    22. const upload = reactive({
    23. name: 'file',
    24. action: '接口地址',
    25. fileList: props.imageUrl ? [{ url: props.imageUrl }] : [],
    26. dialogImageUrl: '',
    27. dialogVisible: false,
    28. before: (file) => {
    29. const type = ['image/png', 'image/jpeg', 'image/gif'];
    30. if (!type.includes(file.type)) {
    31. ElMessage.error('不支持该类型文件')
    32. return false
    33. }
    34. },
    35. proview: ({ url }) => {
    36. console.log(url);
    37. upload.dialogImageUrl = url;
    38. upload.dialogVisible = true;
    39. },
    40. success: (res, uploadFile, uploadFiles) => {
    41. console.log(res);
    42. console.log(uploadFile);
    43. if (res.url && res.code === 1) {
    44. upload.fileList = [{
    45. url: uploadFile.url,
    46. }];
    47. emit('getImageUrl', res.url, uploadFile.url);
    48. // document.querySelector('.el-upload--picture-card').style.display = "none"
    49. } else {
    50. ElMessage({ type: 'warning', message: res.msg });
    51. upload.fileList = [];
    52. emit('getImageUrl', '', '');
    53. }
    54. },
    55. remove: (file, files) => {
    56. upload.fileList = [];
    57. emit('getImageUrl', '', '');
    58. }
    59. })
    60. script>
    61. <style scope lang="scss">
    62. [data-fileListCount='1'] {
    63. .el-upload--picture-card {
    64. display: none !important;
    65. }
    66. }
    67. style>

    页面使用:

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

    模版区使用:

    <UploadAvatar @getImageUrl="getImageUrl" :imageUrl="table.image" />

  • 相关阅读:
    第九章:最新版零基础学习 PYTHON 教程—Python 元组(第五节 -清除元组的5种方式方法)
    获取需要对比的日期范围方法
    nginx的配置文件概述及简单demo(二)
    一文看懂GPT风口,都有哪些创业机会?
    ORB-SLAM2 ---- ORBmatcher::SearchByBoW函数
    【原型链污染】Python与Js
    Electron 两个线程
    详情图怎么做二维码?批量线上图片生码图文教学
    JavaWeb在线商城系统(java+jsp+servlet+MySQL+jdbc+css+js+jQuery)
    文件资源管理器查看不了文件了
  • 原文地址:https://blog.csdn.net/m0_74801194/article/details/133376094