vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效解决方法

我要实现的功能是在上传文件之前校验是否表格中存在重复的数据,有的话,需要弹窗提示是否覆盖,确认之后继续上传,取消之后,就不再上传。
项目中用的element-ui是V1.4.3

<el-upload
      class="upload-demo" drag
     ref="fileUpload"
     :action="urls.fileUpload"
     :on-success="handleUploadSuccess"
     :on-error="handleUploadError"
     :on-progress="progressUpload"
     :before-upload="beforeUpload"
      show-file-list
      multiple>
     <i class="el-icon-upload"></i>
    <div class="el-upload__text">点击上传,或者拖拽到这里</div>
 </el-upload>

代码中我是将before-upload方法返回false,然后点击确认之后,调_this.$refs.fileUpload.submit();但是在点击确定之后,文件还是没有上传,还有第二个问题,就是取消时_this.$refs.fileUpload.clearFiles();我调的clearFiles()方法,这个方法会把文件列表全部清空,我只想删除我当时取消的那个文件。
后面去看了element-ui源码,发现before-upload方法如果返回false,submit()方法是会被拦截的。源码如下:

 

源码中最终是调用了子组件中的post方法,如果不知道是哪个子组件可打印出_this.$refs.fileUpload.$children

于是我将这两句换成了如下图两句:

继续上传: _this.$refs.fileUpload.$children[0].post(file);
取消时在文件列表中删除该文件:_this.$refs.fileUpload.handleRemove(file);

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Element UI是一个基于Vue.js的UI组件库,用于快速构建Web界面。要在Vue项目使用Element UI,需要进行以下步骤: 1. 在项目的main.js文件添加以下代码: ```javascript import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 这将引入Element UI组件库并注册为全局组件。 2. 确保你已经安装了Element UI依赖。可以通过在终端运行以下命令来安装: ``` npm install element-ui --save ``` 这将在你的项目安装Element UI。 3. 在需要使用Element UI组件的地方,可以直接在Vue组件引入所需的组件。例如,在一个Vue组件使用Button组件: ```javascript <template> <div> <el-button>按钮</el-button> </div> </template> <script> import { Button } from 'element-ui' export default { components: { 'el-button': Button } } </script> ``` 这样就可以在Vue项目使用Element UI组件了。记得在需要使用的组件前加上"el-"前缀。 请注意,以上引用的内容是针对Vue 2.0版本和Element UI的使用。如果你使用的是其他版本,请参考相应的文档。 #### 引用[.reference_title] - *1* [Vue2.0Element UI的使用](https://blog.csdn.net/qq_46233520/article/details/122690258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue2.0项目引入element-ui](https://blog.csdn.net/weixin_33905756/article/details/88986106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue2.0安装Element-ui](https://blog.csdn.net/m0_59511468/article/details/124886095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值