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()
            }
        },
相关推荐
树上有只程序猿9 小时前
终于有人把数据库讲明白了
前端
猩兵哥哥9 小时前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
司宸9 小时前
Prompt设计实战指南:三大模板与进阶技巧
前端
RoyLin9 小时前
TypeScript设计模式:抽象工厂模式
前端·后端·typescript
华仔啊9 小时前
Vue3+CSS 实现的 3D 卡片动画,让你的网页瞬间高大上
前端·css
江城开朗的豌豆9 小时前
解密React虚拟DOM:我的高效渲染秘诀 🚀
前端·javascript·react.js
vivo互联网技术10 小时前
拥抱新一代 Web 3D 引擎,Three.js 项目快速升级 Galacean 指南
前端·three.js
江城开朗的豌豆10 小时前
React应用优化指南:让我的项目性能“起飞”✨
前端·javascript·react.js
会飞的青蛙10 小时前
GIT 配置别名&脚本自动化执行
前端·git