vue2项目封装el-upload组件图片上传及删除

用到组件

vue2pc端项目一般搭配使用element组件,图片上传使用Upload组件,如果项目中有大量的图片上传功能,我们可以对Upload组件进行二次封装。

封装图片上传组件

<template>
	<div class="imgContent">
		<el-upload
			class="avatar-uploader
			action=""
			:show-file-list="false"
			:headers="headers"
			before-upload="beforeAvatarUpload
			:http-request="handleUpload"
			>
			<img v-if="value” :src="value” class="avatar" :style="{'width':width,'height':height}">
			<i v-else class="el-icon-plus avatar-uploader-icon" :style="{'width':width,'height':height,'line-height':lineHeight}"></i>
		</el-upload>
		<div v-if="value" class="delete" @click="delImg">
			<i class="el-icon-close"></i></div>
		</div>
</template>
<script>
import [ authuploadFileForCms ] from "@/api/common.js
import {getToken } from "@/utils/auth.js"
export default {
props:{
	 value:{
	  type:string
	  default:''
	},
	 delImg:{
	  type:Function
	  default:()=>{}
	},
	 width: {
      type: String,
      default: '148px'
    },
    height: {
      type: String,
      default: '148px'
    },
    lineHeight: {
      type: String,
      default: '148px'
    }
},
data(){
	return {
	headers:{
		Authorization:"Bearer" +  getToken(),//上传图片请求头携带token
	  }	
	}
  }
},
methods:{
//图片上传前校验
beforeAvatarUpload(file){
	const ispic = ['image/png','image/jpeg', 'image/gif', 'image/bmp', 'image/jpg'].includes(file.type)
	const isLt5M = filesize/1024/1024 < 5
	if(!ispic){
	 this.$message.error("请上传正确的图片格式)
	 return false
	}
	if(!isLt5M){
	  this.$message.error("上传图片大小不能超过5MB')
	  return false
	 }
//将文件读取为DatauRL,以data:开头的字符串
	//const reader = new FileReader();
	//reader.readAsDataURL(file);
	//reader .onload = e => {
	//this.editForm.productIcon = e.target.result
	//}
},
//图片上传,ios和andorid只支持FormData,不支持base64
async handleUpload(obj){
	let formData = new FormData();
	formData.append("file",obj.file);
	formData.append("fileUploadType","RESOURCEBIT FILE");
	console.log(formData.get('file'))//此处打印需要加.get()
	let res = await authUploadFileForCms(formData);
	if(res.code==="0000"){
		this .$emit('input',res.data.fileInfo.fileUri)
	}else{
		this.$message.error("上传失败')
	}
<style scoped lang="scss">
.imgContent{
position:relative;
&:hover .delete{
	display:block;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    /* width: 100px;
    height: 100px;
    line-height: 100px; */
    text-align: center;
  }
  .avatar {
    /* width: 100px;
    height: 100px; */
    display: block;
  }
.delete{
	width: 20px;
	height: 20px;
	background-color: 
	red;color: #fff;
	border-radius: 50%;
	position: absolute;
	left: -10px;top: -10px;
	display: none;
	text-align: center;
	line-height: 20px;
	i{
		vertical-align: 
		middle;font-size: 14px;
		color: #fff;
	}
}
</style>

组件中调用上传图片组件

<template>
	<image-upload v-model="imgUrl" :delImg="delImg" width="100px" height="100px" line-height="100px"></image-upload>
</template>
<script>
	import ImageUpload from "../commonUI/imageUpload .vue"
export default {
components:{
	ImageUpload
 },
data(){
	return {
		imgUrl:''
    },
  methods:{
  	delImg(){
  	this.imgUrl=''
  	}
  }
}
</script>
<style>
 
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于el-upload组件封装,可以按照以下步骤进行: 1. 创建一个自定义组件,可以命名为Upload.vue 或者其他你喜欢的名字。 2. 在该组件中,引入el-upload组件,并根据需要设置相应的属性和事件。 3. 在组件的模板中,使用el-upload组件,并传入相应的props和event。 4. 在父组件中使用该封装组件,并根据需要传递参数和处理事件。 以下是一个简单的el-upload组件封装的示例代码: ```vue <template> <div> <el-upload :action="uploadUrl" :before-upload="handleBeforeUpload" :on-success="handleSuccess" :on-error="handleError" > <el-button size="small" type="primary">点击上传</el-button> </el-upload> </div> </template> <script> export default { data() { return { uploadUrl: 'your_upload_url' }; }, methods: { handleBeforeUpload(file) { // 处理上传前的逻辑,例如限制文件类型、大小等 }, handleSuccess(response, file, fileList) { // 处理上传成功后的逻辑 console.log('上传成功', response); }, handleError(error, file, fileList) { // 处理上传失败后的逻辑 console.log('上传失败', error); } } }; </script> <style> /* 可以根据需要进行样式调整 */ </style> ``` 在父组件中使用该自定义的Upload组件时,可以像普通的组件一样引入并使用: ```vue <template> <div> <Upload></Upload> </div> </template> <script> import Upload from '@/components/Upload.vue'; export default { components: { Upload } }; </script> ``` 这样你就可以在父组件中使用封装el-upload组件了。你可以根据自己的需求,进一步调整和扩展该封装组件的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值