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()
            }
        },
相关推荐
GISer_Jing1 分钟前
前端营销I(From AIGC)
前端·aigc·ai编程
明月_清风9 分钟前
向 Native 借力:深度拆解 SIMD 加速与 Node.js 异步原生解析
前端·json
明月_清风15 分钟前
无感监控:深度拆解监控 SDK 的性能平衡术与调度策略
前端·监控
键盘鼓手苏苏2 小时前
Flutter for OpenHarmony:markdown 纯 Dart 解析引擎(将文本转化为结构化 HTML/UI) 深度解析与鸿蒙适配指南
前端·网络·算法·flutter·ui·html·harmonyos
芭拉拉小魔仙8 小时前
企业级Vue项目的状态管理:从原理到实战架构
前端·vue.js·架构
恋猫de小郭8 小时前
丰田正在使用 Flutter 开发游戏引擎 Fluorite
android·前端·flutter
扶苏10029 小时前
Vue 3 响应式原理深度解析
前端·javascript·vue.js
NEXT069 小时前
React 性能优化:图片懒加载
前端·react.js·面试
PineappleCoder9 小时前
别让字体拖了后腿:FOIT/FOUT 深度解析与字体加载优化全攻略
前端·性能优化
NEXT0610 小时前
后端跑路了怎么办?前端工程师用 Mock.js 自救实录
前端·后端·程序员