element-ui中el-radio-group组件绑定点击事件触发多次的解决办法

1、需求

电商首页需求,需要做个单选框,然后点击选中切换图标方向及更换价格升倒序,如下图:


从官网文档看,单选框支持change event,使用click 加载按钮处不会触发选中

但是使用 @click.native 事件不做处理的话会发送多次请求

解决方案如下:

bash 复制代码
<el-radio-group v-model="buttonRightRadio" id="buttonRightRadio">
                        <el-radio-button label="default" @click.native="sortBy('default',$event)">默认</el-radio-button>
                        <el-radio-button label="sales" @click.native="sortBy('sales',$event)">销量
                            <i :class="{'el-icon-top': sortType === 'sales' && sortDirection === 'desc', 'el-icon-bottom': sortType === 'sales' && sortDirection === 'asc'}"></i>
                        </el-radio-button>
                        <el-radio-button label="price" @click.native="sortBy('price',$event)">价格
                            <i :class="{'el-icon-top': sortType === 'price' && sortDirection === 'desc', 'el-icon-bottom': sortType === 'price' && sortDirection === 'asc'}"></i>
                        </el-radio-button>
                    </el-radio-group>
bash 复制代码
  sortBy(type, event) {
  //此处解决发送多次请求
            if (event.target.tagName != 'INPUT') {
                return;
            }
            if (type === 'default') {
                this.sortType = '';
                this.sortDirection = '';
                this.load();
                return;
            }
            if (this.sortType === type) {
                // 如果当前已经是该排序方式,则切换排序方向
                this.sortDirection = this.sortDirection === 'desc' ? 'asc' : 'desc';
                this.load()
            } else {
                // 如果当前不是该排序方式,则设置为该排序方式,并将排序方向设为降序
                this.sortType = type;
                this.sortDirection = 'desc';
                this.load()
            }
        },
相关推荐
子兮曰18 分钟前
Bun v1.3.12 深度解析:新特性、性能优化与实战指南
前端·typescript·bun
2401_885885041 小时前
易语言彩信接口怎么调用?E语言Post实现多媒体数据批量下发
前端
a1117761 小时前
Three.js 的前端 WebGL 页面合集(日本 开源项目)
前端·javascript·webgl
Kk.08021 小时前
项目《基于Linux下的mybash命令解释器》(一)
前端·javascript·算法
小李子呢02112 小时前
前端八股---闭包和作用域链
前端
IT_陈寒3 小时前
Redis的内存溢出坑把我整懵了,分享这个血泪教训
前端·人工智能·后端
m0_738120723 小时前
渗透测试基础ctfshow——Web应用安全与防护(五)
前端·网络·数据库·windows·python·sql·安全
Z_Wonderful3 小时前
基于 Vite 的 React+Vue 混部完整模板(含目录结构、依赖清单、启动脚本)
前端·vue.js·react.js
Rooting++3 小时前
腾讯无界微前端源码分析
前端
小嘿前端仔3 小时前
用AI读源码这件事:前端视角的实战方法论,附Vue3 reactivity源码解读示范
前端