一、单张图片上传
组件 Upload.vue
<template>
<div>
<el-upload
class="pic-uploader-component"
action="上传地址"
:show-file-list="false"
:on-success="handleUploadSuccess"
:on-change="handleChange"
:before-upload="beforeAvatarUpload"
>
<img v-if="value && !loading" :src="resourcesUrl + value" class="pic">
<i v-if="!value && !loading" class="el-icon-plus pic-uploader-icon" />
<i v-if="loading" class="el-icon-loading pic-uploader-icon" />
</el-upload>
</div>
</template>
<script>
export default {
props: {
value: {
default: '',
type: String
}
},
data() {
return {
loading: false,
resourcesUrl: '',
}
},
methods: {
handleChange(){
this.loading = false
},
// 图片上传
handleUploadSuccess(response, file, fileList) {
console.log(file)
this.$emit('input', file.response)
this.loading = false
},
// 限制图片上传大小
beforeAvatarUpload(file) {
this.loading = true
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 2MB!')
}
return isLt2M
}
}
}
</script>
<style lang="scss">
.pic-uploader-component .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
.pic-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 120px;
height: 120px;
line-height: 120px;
text-align: center;
}
.pic {
width: 120px;
height: 120px;
display: block;
}
}
.pic-uploader-component .el-upload:hover {
border-color: #409EFF;
}
</style>
引入注册后的使用方式
<upload v-model="url"></upload>
Vue2.x二次封装el-upload组件

这篇博客主要介绍了在Vue2.x中如何对element-ui的el-upload组件进行二次封装,详细展示了单张图片上传的实现过程,包括Upload.vue组件的编写以及在项目中的引入和使用方法。多张图片上传的功能目前处于待更新状态。
826

被折叠的 条评论
为什么被折叠?



