ElementUI upload弹出两次文件选择

前提

Vue 3.0 + Element Plus

使用手动上传,写了两个按钮,分别是“上传文件”和“进入分析”

预想

点击“上传文件”会弹出文件选择器进行文件选择

点击“进入分析”会调用函数将文件传送到后端

问题

点击“进入分析”后也会弹出文件选择器

原因分析

两个按钮共用一个input,会触发两次inputclick事件

参考 https://www.cnblogs.com/naxwnm/p/14277822.html

原代码

HTML

<el-upload ref="upload" v-model:file-list="fileList" class="upload-file" :limit="3" multiple action="/upload"
    :on-exceed="handleExceed" :before-upload="fileFilter" :on-success="fileUploaded" :auto-upload="false"
    :data="uploadParam">
    <el-button type="primary" slot="trigger">点击上传</el-button>
    <el-button style="margin-left: 2em;" type="success" @click="submitUpload">进入分析</el-button>
    <template #tip>
        <div class="el-upload__tip">
            仅支持json文件
        </div>
    </template>
</el-upload>

JavaScript

export default {
    methods: {
        submitUpload() {
            this.$refs.upload.submit();
        },
    }
}

修改后

仅修改HTML代码,加一层template

<el-upload ref="upload" v-model:file-list="fileList" class="upload-file" :limit="3" multiple action="/upload"
    :on-exceed="handleExceed" :before-upload="fileFilter" :on-success="fileUploaded" :auto-upload="false"
    :data="uploadParam">
    <template #trigger>
        <el-button type="primary">点击上传</el-button>
    </template>
    <el-button style="margin-left: 2em;" type="success" @click="submitUpload">进入分析</el-button>
    <template #tip>
        <div class="el-upload__tip">
            仅支持json文件
        </div>
    </template>
</el-upload>

补充

在评论区中用户Moirrra提出了一种更简单的解决方法,就是把”进入分析“那个button的@click改为@click.stop即可,修改后如下

<el-upload ref="upload" v-model:file-list="fileList" class="upload-file" :limit="3" multiple action="/upload"
    :on-exceed="handleExceed" :before-upload="fileFilter" :on-success="fileUploaded" :auto-upload="false"
    :data="uploadParam">
    <el-button type="primary" slot="trigger">点击上传</el-button>
    <el-button style="margin-left: 2em;" type="success" @click.stop="submitUpload">进入分析</el-button>
    <template #tip>
        <div class="el-upload__tip">
            仅支持json文件
        </div>
    </template>
</el-upload>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值