解决elementPlus的el-upload上传组件,点击预览查看大图,关闭预览大图,自动触发弹出选择图片上传的问题

由于需求是新增/编辑以dialog展示form表单,上传图片功能,其他都正常但是在el-upload上传图片组件中点击预览查看大图然后再关闭预览的时候就会自动触发上传图片选择文件,这个问题一开始我以为是el-dialog嵌套的问题,以这个方向百度很长时间一直没有得到解决

 仔细看文档对比发现是el-upload嵌套el-dialog的问题

修改后

 问题就解决了
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现el-upload上传文件后点击预览的功能,可以通过以下步骤实现: 1. 在el-upload组件中添加一个自定义的slot,用于显示预览按钮。 2. 在handleFileChange函数中,将上传的文件保存到一个数组中,同时为每个文件生成一个唯一的id。 3. 在预览按钮的点击事件中,根据文件的id获取对应的文件内容,并将其显示在一个弹窗中。 4. 在弹窗中添加一个删除按钮,用于删除当前预览的文件。 代码示例: ``` <el-upload class="upload-demo" ref="uploadFile" :auto-upload="false" :action="testForm.actionUrl" drag :file-list="testForm.fileList" :on-change="handleFileChange" :on-remove="removeFile" :limit="1" :on-exceed="overLimitCount" > <el-button slot="trigger" size="small" type="primary">选择文件</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> <el-button slot="preview" size="small" type="text" @click="showPreview">预览</el-button> </el-upload> data() { return { testForm: { fileList: [], fileContentList: [] }, previewFileId: '', isShowPreview: false } }, methods: { handleFileChange(files, fileList) { this.testForm.fileList = fileList files.forEach(file => { const reader = new FileReader() reader.readAsText(file.raw) reader.onload = e => { const fileContent = e.target.result.replace(/\n|\r\n/g, '<br/>') this.testForm.fileContentList.push({ id: file.uid, content: fileContent }) } }) }, showPreview() { if (this.testForm.fileContentList.length > 0) { this.previewFileId = this.testForm.fileContentList[0].id this.isShowPreview = true } }, removeFile(file, fileList) { const index = this.testForm.fileContentList.findIndex(item => item.id === file.uid) if (index !== -1) { this.testForm.fileContentList.splice(index, 1) } }, removePreview() { const index = this.testForm.fileContentList.findIndex(item => item.id === this.previewFileId) if (index !== -1) { this.testForm.fileContentList.splice(index, 1) this.isShowPreview = false } } }, computed: { previewFileContent() { const file = this.testForm.fileContentList.find(item => item.id === this.previewFileId) return file ? file.content : '' } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值