element-ui upload 组件附带额外参数进行上传(一个或多个参数)

element-ui upload 组件附带额外参数进行上传(一个或多个参数),实参、形参均可以传。

正常情况:

在这里插入图片描述

附带额外参数进行上传(一个或多个参数)时-------使用箭头函数:

HTML部分:

 <el-upload 
    <!-- 使用箭头函数,实现传递一个或多个额外参数功能,'1'为额外参数 -->
    :on-success="(response, file, fileList)=>onSuccess(response, file, fileList,'1')" 
    <!-- 文件上传失败时的钩子 -->
    :on-error="onError" 
    <!-- 文件上传的地址 -->
    :action="'/api/upload/'"
    <!-- 上传的文件列表 -->
    :file-list="fileLists" 
    <!-- 使用箭头函数,实现传递一个或多个额外参数功能,'1'为额外参数 -->
    :on-remove="(file, fileList)=>handleRemove(file, fileList,'1','2')">
    <el-button size="mini" type="text">上传</el-button>
</el-upload>

JavaScript部分:

 onSuccess(response, file, fileList, val) {
     console.log('onSuccess额外参数val------',val);    // 1
}
 handleRemove(file, fileList, val1,val2) {
     console.log('handleRemove额外参数val1------',val1);    // 1
     console.log('handleRemove额外参数val2------',val2);    // 2
}

其他方法也是一样的。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 Element UI 二次封装的 Upload组件的示例代码: ``` <template> <div> <el-upload :action="uploadUrl" :headers="headers" :data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :on-exceed="onExceed" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :on-progress="onProgress" > <el-button type="primary" :disabled="uploading">{{ buttonText }}</el-button> <div slot="tip" class="el-upload__tip">{{ tip }}</div> </el-upload> </div> </template> <script> export default { name: "MyUpload", props: { uploadUrl: { type: String, default: "" }, headers: { type: Object, default: () => ({}) }, formData: { type: Object, default: () => ({}) }, multiple: { type: Boolean, default: false }, showFileList: { type: Boolean, default: true }, limit: { type: Number, default: 0 }, buttonText: { type: String, default: "上文件" }, tip: { type: String, default: "只能上jpg/png文件,且不超过500kb" } }, data() { return { uploading: false }; }, methods: { onExceed(files, fileList) { this.$message.warning(`只能上${this.limit}个文件`); }, beforeUpload(file) { const isJPG = file.type === "image/jpeg" || file.type === "image/png"; const isLt500K = file.size / 1024 < 500; if (!isJPG) { this.$message.error("上图片只能是 JPG/PNG 格式!"); return false; } if (!isLt500K) { this.$message.error("上图片大小不能超过 500KB!"); return false; } return true; }, onSuccess(response, file, fileList) { this.uploading = false; this.$emit("upload-success", response, file, fileList); }, onError(err, file, fileList) { this.uploading = false; this.$emit("upload-error", err, file, fileList); }, onProgress(event, file, fileList) { this.uploading = true; this.$emit("upload-progress", event, file, fileList); } } }; </script> <style> /* 可以根据自己的需要修改样式 */ .el-upload__tip { font-size: 14px; color: #999; margin-top: 10px; } </style> ``` 这个 Upload 组件支持以下 props: - `uploadUrl`:上文件的接口地址 - `headers`:上请求的 headers - `formData`:上请求的 formData - `multiple`:是否支持多选文件 - `showFileList`:是否显示已上文件列表 - `limit`:最多上文件个数 - `buttonText`:上按钮的文本 - `tip`:上提示信息 这个 Upload 组件还支持以下事件: - `upload-success`:上成功的回调函数,参数为 response、file 和 fileList - `upload-error`:上失败的回调函数,参数为 err、file 和 fileList - `upload-progress`:上进度的回调函数,参数为 event、file 和 fileList 使用示例: ``` <template> <div> <my-upload :upload-url="uploadUrl" :headers="headers" :form-data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :button-text="buttonText" :tip="tip" @upload-success="onUploadSuccess" @upload-error="onUploadError" @upload-progress="onUploadProgress" ></my-upload> </div> </template> <script> import MyUpload from "@/components/MyUpload"; export default { components: { MyUpload }, data() { return { uploadUrl: "https://xxx.com/upload", headers: { token: "xxx" }, formData: { type: "avatar" }, multiple: false, showFileList: true, limit: 1, buttonText: "上头像", tip: "只能上jpg/png文件,且不超过500kb" }; }, methods: { onUploadSuccess(response, file, fileList) { console.log("上成功", response, file, fileList); }, onUploadError(err, file, fileList) { console.log("上失败", err, file, fileList); }, onUploadProgress(event, file, fileList) { console.log("上进度", event, file, fileList); } } }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值