vue3如何二次封装el-upload组件进行图片上传及删除

实现功能:
1、封装el-upload组件,父组件可以控制图片上传框的宽高
2、父组件可以传入提示信息,利用具名插槽
3、上传前的校验
4、实现删除功能

不同配置下的效果:
在这里插入图片描述
下边案例是图片上传的时候没有掉接口,在整体提交的时候才调用的接口,所以需要一个中间变量去接收他的图片回显
父组件引用:

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/stores';
const userStore = useUserStore()
const img = ref(userStore.user.user_pic)
const selectRef = ref(null)
//1、调用el-upload中on-change事件
// const triggerChange = ()=>{
//   if(selectRef.value){
//     const file = new File ([""],"filename")
//     selectRef.value.onSelectFile(file,[])
//   }
// }

//2、点击按钮调用子组件,图片上传选择文件弹出框
const openFileDialog  =()=>{
  selectRef.value.$refs.uploadRef.$el.querySelector('input').click()
}
</script>

<template>
  <pageContainer title="更换头像">
     <uploadImg 
      v-model="img" ref="selectRef"
      width="200px" 
      height="200px" 
      line-height="200px"
    >
      <!-- 具名插槽 -->
      <template #tips>
      <div style="margin-top:10px;">
        <el-button type="primary" size="small" 
        @click="openFileDialog">更换头像</el-button>
        <el-button type="success" size="small" >上传头像</el-button>
      </div>
    </template>
  </uploadImg>
  </pageContainer>
</template>
<style scoped>
</style>

子组件:

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus,Close } from '@element-plus/icons-vue'
const imageUrl = ref('')
const props = defineProps({
    modelValue:{
        type:String
    },
    width:{
      type:String,
      default: '180px'
    },
    height:{
      type:String,
      default: '180px'
    },
    lineHeight:{
      type:String,
      default: '180px'
    }
})
const uploadRef = ref()
// 1、不是在上传的时候调接口而是本地上传,提交表单时再调取接口
// 所以再没有数据的时候并且modelValue传过来的是一个空对象的时候要将图片回显置空
const handleImg = ()=>{
  imageUrl.value=props.modelValue
  if(JSON.stringify(imageUrl.value) == "{}"){
    imageUrl.value=''
  }
}
handleImg()
const emit =defineEmits(['update:modelValue'])
const onSelectFile = (uploadFile) => {
  //2、上传前校验
  const imgType = ['image/png','image/jpeg', 'image/gif', 'image/bmp', 'image/jpg'] 
  const isLt5M = uploadFile.raw.size/1024/1024 < 2
  if(!imgType.includes(uploadFile.raw.type)){
    return ElMessage.error('请上传正确的图片格式')
  }
  if(!isLt5M){
    return ElMessage.error('上传的图片太大了')
  }
  //3、转成在线地址实现预览
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  //4、改变父组件的值
  emit('update:modelValue',uploadFile.raw) 
}
defineExpose({
  onSelectFile,
})
//删除图片
const deleteImg = ()=>{
  imageUrl.value = ''
  emit('update:modelValue','') 
}
</script>

<template> 
    //1、此处关闭自动上传,:auto-upload="false"
    //2、不需要action,本地上传,提交时再上传图片
    //3、URL.createObjectURL(...)创建本地预览的地址,此处只本地预览,点击提交的时候,再整个form调接口。
    //4、如果上传图片的时候就调接口,用:http-request="uploadImgage"
    //5、如果有:auto-upload="false" 属性就要用:on-change 方法监听
    const uploadImgage = async(file) => {
      // 请求接口中需要带url以及
      let result = await proxy.Request({
          url:api.uploadUrl,  // 上传路径
          dataType:'file', // 指定传输类型
          params:{
              file:file.file,   // 提交上传路径到指定位置
              type:0,          
          }
      })
        const fileName = result.data.fileName
        emit('update:modelValue',fileName) 
    };

<div class="imgContent" :style="{'width':width,'height':height}">
 <el-upload
    class="avatar-uploader"
    :show-file-list="false"
    :auto-upload="false"
    :on-change="onSelectFile"
    ref="uploadRef"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" :style="{'width':width,'height':height}"/>
    <el-icon v-else class="avatar-uploader-icon" :style="{'width':width,'height':height,'line-height':lineHeight}"><Plus /></el-icon>
  </el-upload>
  <div v-if="imageUrl"  class="delete" @click="deleteImg">
    <el-icon><Close /></el-icon>
  </div> 
</div>
	<slot name="tips">默认内容</slot>
</template>

<style scoped lang="scss">
.imgContent{
  position: relative;
  &:hover .delete{
	display:block;
}
}
.delete{
  position: absolute;
  display: none;
  width:20px;
  height:20px;
  top:-8px;
  left:-8px;
  background-color: red;
  border-radius: 50%;
  text-align: center;
	line-height: 24px;
}
.el-icon{
  color:#fff;
  font-weight: 700;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue3和Element UI的el-upload二次封装,你可以参考以下步骤: 1. 首先,根据Element UI官方文档的指引,了解el-upload组件的使用方法和属性。你可以在官方文档中找到详细的代码示例和解释。 2. 创建一个Vue组件,例如UploadImg,用于封装el-upload组件。在组件中,你可以根据需要使用el-upload提供的上传、文件列表、删除等功能。你可以根据自己的需求,将el-upload的属性传递给子级组件或自定义函数进行处理。 3. 在组件中,你可以使用axios或其他HTTP库来进行文件的上传和删除操作。你可以根据自己的需要,封装适当的请求函数。比如,你可以创建一个delUpImage函数用于删除图片,或者创建一个uploadImg函数用于上传图片。 4. 在Vue的template中使用封装好的UploadImg组件,传递相应的属性和事件监听函数。这样,你就可以在页面中使用二次封装el-upload组件了。 下面是一个示例的代码结构: ```javascript // UploadImg.vue <template> <el-upload :limit="limit" action="" accept="image/*" :http-request="uploadFile" list-type="picture-card" :auto-upload="false" :file-list="fileList" :on-exceed="handleExceed" :before-remove="beforeRemove" ref="upload" > <img src="../assets/common_images/uploadImage.png" width="146" height="146"> </el-upload> <el-form-item> <el-button size="small" type="primary" style="margin-top: 20px;" @click="submitUpload">点击上传</el-button> </el-form-item> </template> <script> import { defineComponent } from 'vue'; import axios from 'axios'; export default defineComponent({ name: 'UploadImg', props: { limit: { type: Number, default: 6 }, fileList: { type: Array, default: () => [] } }, methods: { uploadFile(file) { // 上传文件的逻辑处理 }, handleExceed(files, fileList) { // 处理文件超出限制的逻辑 }, beforeRemove(file, fileList) { // 删除文件前的逻辑处理 }, submitUpload() { // 点击上传按钮的逻辑处理 } } }); </script> ``` 请注意,这只是一个示例代码,你需要根据自己的实际需求进行相应的修改和调整。另外,为了实现文件上传和删除操作,你可能还需要引入其他库或自定义函数来处理HTTP请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值