夜猫子的知识栈 夜猫子的知识栈
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

夜猫子

前端练习生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • 博客搭建

  • Ajax

  • Vite

  • Vitest

  • Nuxt

  • UI库文章

    • iconify
    • elelmentPlus
      • upload组件封装
    • echarts的使用
    • antDesign
    • antDesignVue
  • Docker

  • 技术
  • UI库文章
夜猫子
2022-08-25
目录

elelmentPlus

# upload组件封装

<template>
    <div>
        <el-upload v-model:file-list="fileList" :accept="props.accept" action="#" list-type="picture-card"
            :auto-upload="true" :limit="props.numLimit" :http-request="uploadpromise">
            <template #file="{ file }">
                <div class="upload">
                    <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                    <span class="el-upload-list__item-actions">
                        <span class="upload-delete" @click="handleRemove(file)">
                            删除
                        </span>
                    </span>
                </div>
            </template>
            <div class="imgtip">
                <div class="addiccon">+</div>
                <div class="title">上传照片</div>
            </div>
        </el-upload>
    </div>
</template>

<script  setup>
import { defineEmits, ref, defineProps, watch } from "vue"
import { ElMessage } from 'element-plus'
const fileList = ref([])
const props = defineProps({
    accept: {
        type: String,
        default: "image/jpeg,image/png,image/gif,image/jpg,image/bmp"
    },
    sizeLimit: {
        type: [String, Number],
        default: 8
    },
    numLimit: {
        type: [String, Number],
        default: 1
    },
    width: {
        type: [String, Number],
        default: '104px',
        validator(value) {
            return (!isNaN(parseFloat(value)) && isFinite(value)) || ['px', 'em', 'rem', 'vw'].some(item => {
                return value.indexOf(item) > -1
            })
        }
    },
    height: {
        type: [String, Number],
        default: '104px',
        validator(value) {
            return (!isNaN(parseFloat(value)) && isFinite(value)) || ['px', 'em', 'rem', 'vh'].some(item => {
                return value.indexOf(item) > -1
            })
        }
    }
})

const style = {
    width: !isNaN(parseFloat(props.width)) && isFinite(props.width) ? props.width + 'px' : props.width,
    height: !isNaN(parseFloat(props.height)) && isFinite(props.height) ? props.height + 'px' : props.height
}

const emit = defineEmits(['uploaded'])

watch(fileList, (newValue) => {
    emit('uploaded', newValue)
})

const listLimit = (file) => {
    const typeLimitBool = props.accept.split(',').some(_type => file.type === _type);
    if (!typeLimitBool) {
        ElMessage.error('请上传格式正确的图片');
    }

    const sizeLimitBool = file.size / 1024 / 1024 < props.sizeLimit;
    if (!sizeLimitBool) {
        ElMessage.error(`请使用小于${props.sizeLimit}MB的图片!`);
    }
    let result = sizeLimitBool && typeLimitBool;
    return result
}
const uploadpromise = (param) => {
    const file = param.file
    return new Promise((resolve, reject) => {
        if (listLimit(file)) { resolve(true) } else {
            reject(new Error('格式不正确'));
        }
    })
}
const handleRemove = (file) => {
    const fileID = file.uid
    // 从数组中,找到图片对应的索引值
    const i = fileList.value.findIndex(x => x.uid === fileID)
    // 调用splice方法,移除图片信息
    fileList.value.splice(i, 1)
    emit('uploaded', fileList.value)
}


</script>

<style lang="less" scoped>
:deep(.el-upload-list__item-thumbnail),
:deep(.el-upload-list__item),
:deep(.el-upload--picture-card) {
    width: v-bind("style.width");
    height: v-bind("style.width");
}

.imgtip {
    .addiccon {
        margin-left: 10px;
        color: rgba(0, 0, 0, 0.65);
        font-size: 50px;
        // overflow: hidden;
    }

    .title {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.65);
        text-align: center;
        line-height: 22px;
    }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
编辑 (opens new window)
上次更新: 2023/7/11 18:57:41
iconify
echarts的使用

← iconify echarts的使用→

最近更新
01
IoC 解决了什么痛点问题?
03-10
02
如何调试 Nest 项目
03-10
03
Provider注入对象
03-10
更多文章>
Copyright © 2019-2025 Study | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式