vue 使用element-ui upload文件上传之后怎么清空

首先上传组件中一定要绑定这两个属性:

ref,和 :file-list,如果没有ref,即使 用 this.$refs.upload.clearFiles()也不行,因为这时候this.$refs为空对象,绑定完之后可以把this.$refs打印出来看看,如下图:

 

 这时候再用this.$refs.upload.clearFiles()就可以把图片清空了

转载于:https://www.cnblogs.com/lyyguniang/p/9850696.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用vueelement-ui制作上传文件的效果时,可以按照以下步骤进行: 1. 安装element-ui:在终端中使用npm或yarn进行安装,例如:npm install element-ui --save。 2. 在vue组件中引入element-ui上传组件:在需要使用上传功能的组件中,使用import引入element-ui上传组件,例如: ``` <template> <div> <el-upload action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :file-list="fileList" multiple list-type="picture" :auto-upload="false" :on-success="handleUploadSuccess" :on-error="handleUploadError"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> import { Upload, Button } from 'element-ui'; export default { components: { 'el-upload': Upload, 'el-button': Button }, data() { return { fileList: [] }; }, methods: { handlePreview(file) { console.log('preview', file); }, handleRemove(file) { console.log('remove', file); }, 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文件'); } if (!isLt500k) { this.$message.error('文件大小不能超过500kb'); } return isJPG && isLt500k; }, handleUploadSuccess(response, file, fileList) { console.log('success', response, file, fileList); }, handleUploadError(error, file, fileList) { console.log('error', error, file, fileList); } } }; </script> ``` 3. 配置上传组件的属性和方法:在引入上传组件后,需要根据实际需求进行配置,例如:设置上传的地址、文件大小限制、文件类型限制、上传前的校验、上传成功和失败的回调函数等。 4. 在模板中使用上传组件:在组件的模板中使用上传组件,并将需要上传的文件绑定到fileList属性上,例如:`:file-list="fileList"`。 5. 样式美化:根据实际需求对上传组件的样式进行美化,例如:设置上传按钮的颜色、设置文件列表的样式等。 通过以上步骤,就可以在vue项目中使用element-ui制作上传文件的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值