elementUI上传组件封装

本文将介绍如何对Element UI的上传组件进行封装,以实现更灵活的图片上传功能。通过创建`imageMine.vue`,我们可以定制上传的样式、增加预览、裁剪等特性,提升用户体验。

elementUI上传组件封装

<template>
  <div class="c-upload-wrapper c-text-align upload-image-box">
    <template v-if="acceptType.split('image').length === 2">
      <template v-for="(item, index) in uploadFileBase64">
        <image-mine
          :key="index"
          :style="`width: ${options.width}px; height: ${options.height}px`"
          v-if="options.isNeedBig && item.url"
          class="c-upload-size"
          lazy="true"
          :src="item.url | isBaseImg"
          :previewSrcList="[item.url]"
        />
      </template>
    </template>
    <template v-else>
      <div v-if="uploadFileBase64.length > 0 && text != '上传图标'" class="c-upload-size c-text-align c-text-pack" :style="`display: inline-flex; width: ${options.width}px; height: ${options.height}px; font-size: ${options.signFontSize}px; justify-content: flex-start; padding: 0 10px`">
        <p v-for="(item, index) in uploadFileBase64" :key="index" style="margin-right: 5px">{
  
  {item.name}}{
  
  {index == uploadFileBase64.length - 1 ? '' : ', '}}</p>
      </div>
    </template>
    <el-upload
      class="c-text-start"
      ref="upload"
      action
      :multiple="options.isMultiple"
      :accept="acceptType"
      :auto-upload="false"
      :show-file-list="false"
      :on-change="imgPreview"
    >
      <template v-for="(item, index) in uploadFileBase64">
        <image-mine
          :key="index"
          :style="`width: ${options.width}px; height: ${options.height}px`"
          v-if="!options.isNeedBig && 
封装 Element UI 的上传组件,你可以创建一个自定义的 Vue 组件,然后在其中使用 Element UI 的 el-upload 组件。以下是一个简单的封装示例: ```vue <template> <div> <el-upload ref="upload" :action="uploadUrl" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="autoUpload" > <slot></slot> </el-upload> </div> </template> <script> export default { name: 'CustomUpload', props: { uploadUrl: { type: String, required: true }, autoUpload: { type: Boolean, default: true } }, methods: { handleSuccess(response, file, fileList) { // 处理上传成功的回调逻辑 this.$emit('upload-success', response, file, fileList); }, beforeUpload(file) { // 可以在这里添加一些上传前的逻辑判断,如文件类型、大小等 return true; // 返回 true 表示允许上传 }, clearFiles() { this.$refs.upload.clearFiles(); }, abortUpload() { this.$refs.upload.abort(); } } }; </script> ``` 在这个示例中,我们创建了一个 `CustomUpload` 组件,它接受一个 `uploadUrl` 属性来指定上传地址,并且还有一个可选的 `autoUpload` 属性来控制是否自动上传。 你可以在组件中添加自定义的逻辑和样式,并使用 `this.$refs.upload` 来访问 Element UI 的上传组件实例,从而调用其方法和获取上传的文件信息。 使用示例: ```vue <template> <div> <custom-upload upload-url="上传地址" :auto-upload="false" @upload-success="handleUploadSuccess" > <el-button size="small" type="primary">点击上传</el-button> </custom-upload> </div> </template> <script> import CustomUpload from '@/components/CustomUpload.vue'; export default { components: { CustomUpload }, methods: { handleUploadSuccess(response, file, fileList) { console.log('上传成功'); } } }; </script> ``` 在上面的示例中,我们在 `CustomUpload` 组件中使用了一个插槽,用来插入自定义的上传按钮。当文件上传成功后,会触发 `upload-success` 事件,你可以在父组件中监听该事件并处理相应的逻辑。 这样,你就可以通过封装 Element UI 的上传组件来方便地在项目中使用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值