2.Upload上传组件自定义上传实例及注意点

本文介绍了在Vue项目中如何自定义实现上传组件,重点在于使用`http-request`事件覆盖默认上传行为,通过`this.$refs.upload.submit()`触发上传。提供了一个单张图片上传的实例,并提示图片预览是依赖于`this.form.fileList`变量。
摘要由CSDN通过智能技术生成

1.自定义上传时,当选中图片this.$refs.upload.submit() 就会触发:http-request = "httpRequest"事件 。

<el-form-item :label="upload_lable" :label-width="formLabelWidth">
  <el-upload class="upload-demo" ref="upload" :limit="upload_limit" :auto-upload="false" :http-request="httpRequest" action=""  :on-change="filechange" :on-remove="handleRemove" :file-list="form.fileList" list-type="picture">
    <el-button size="small" type="primary">点击选择</el-button>
    <div slot="tip" class="el-upload__tip">只能上传1张jpg/png文件,且不超过500kb</div>
  </el-upload>
</el-form-item>

<div slot="footer" class="dialog-footer">
  <el-button @click="dialogFormVisible = false">取 消</el-button>
  <el-button type="primary" @click="commitSave">确 定</el-button>
</div>

 

filechange(file, fileList) { // 如果没有选取文件,可以加个字段来控制流程
  if (file.name) {
    this.upload_flag = true;
  }
},
//触发 httpRequest 事件
commitSave(){ // 点确定时触发事件
  if(this.upload_flag){
    this.$refs.upload.submit();
  }else{ // 不传图片,直接调用
    this.$refs['form'].validate((valid) => {
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值