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()
            }
        },
相关推荐
货拉拉技术1 小时前
货拉拉开源Hadice:安卓 & 鸿蒙桌面调试工具
前端
程序猿DD1 小时前
OctaFuse Gateway 2.11.0:流式请求优化、用户折扣策略与错误契约升级
前端·后端
尾善爱看海2 小时前
《JavaScript 数组操作全攻略:12 类 API + 30 个实战场景 + 20 个避坑指南》
前端·javascript·面试
计算机魔术师2 小时前
Anthropic 估值冲 4 万亿,但企业客户已经偷偷换了便宜模型
前端
王六米。2 小时前
RAG知识库建设 文档解析 切分与索引如何衔接
服务器·前端·网络·企业数字化转型·智钳智能盒子
名字还没想好☜2 小时前
React 文件上传实战:预览 URL 回收、多文件逐个进度与拖拽放置区踩坑
前端·react·next.js
console.log('npc')2 小时前
06 — Model 层:数据模型与操作
前端·后端·node.js·express
linux_cfan2 小时前
16 · `custom-media-element`:属性拦截与转发
前端·javascript·音视频
右耳朵猫AI3 小时前
Web前端周刊2026W38 | React 19.3 发布、StyleX 深潜、jsdom 30.1 提速
前端·javascript·react.js·typescript·node.js
人工智能培训3 小时前
大语言模型:从语言理解到通用智能的跃迁
linux·服务器·前端·人工智能