el-upload基础封装

element-plus 文件上传基础封装

对项目中用到的上传组件,做一个简单的封装

<template>
  <el-upload
    ref="uploadRef"
    :action="action"
    :list-type="listType"
    :limit="limit"
    :accept="accept"
    :on-remove="handleRemove"
    :on-success="handleSuccess"
    :before-upload="beforeUpload"
    :on-exceed="handleExceed"
  >
    <el-button type="primary">上传</el-button>
  </el-upload>
</template>

<script setup>
/**
 * 文件上传
 */
import { ElNotification } from '@jlz/element-plus';
import { ref } from 'vue';
const props = defineProps({
  action: {
    type: String,
    default: '/file/local/upload',
  },
  listType: {
    type: String,
    default: 'picture-card',
  },
  // 默认为0上传数量无限制
  limit: {
    type: Number,
    default: 0,
  },
  accept: {
    type: String,
    default: '',
  },
});

const emit = defineEmits(['uploadedList']);
const uploadRef = ref();

function handleExceed() {
  ElNotification({
    title: '操作有误',
    message: `已超出限制,上传最多数量为${props.limit}`,
    type: 'warning',
  });
}

function handleSuccess(_, uploadFile, uploadFiles) {
  const resArr = uploadFiles.map((item) => item.response.data);
  emit('uploadedList', resArr);
}

// 删除前的回调
function handleRemove(uploadFile, uploadFiles) {
  const resArr = uploadFiles.map((item) => item.response.data);
  emit('uploadedList', resArr);
}

// 上传之前,可在此做文件限制
function beforeUpload() {
  // console.log(file);
}

</script>

<style lang="scss" scoped>
// aa
</style>

页面使用

<Uploader
    :list-type="'text'"
    :accept="'.pdf'"
    :limit="1"
    @uploaded-list="uploadedList"
/>
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值