当前位置: 首页 > news >正文

ElementUI el-select 组件动态设置disabled后,高度变更的问题解决办法

问题描述

Vue2 项目在使用 el-select 组件时,动态将disabled变更为了 true,元素的高度发生了变化。
在这里插入图片描述

问题原因

通过浏览器开发人员工具面板,发现,组件内的 input 元素被动态设置了height的样式:
在这里插入图片描述

在项目中检查后并没有找到触发这个设置的代码,所以怀疑是组件自身的逻辑。

于是找到 ElSelect 的源码(node_modules\element-ui\packages\select\src\select.vue),果然发现有一个方法里进行了 height 的设置:

resetInputHeight() {if (this.collapseTags && !this.filterable) return;this.$nextTick(() => {if (!this.$refs.reference) return;let inputChildNodes = this.$refs.reference.$el.childNodes;let input = [].filter.call(inputChildNodes, item => item.tagName === 'INPUT')[0];const tags = this.$refs.tags;const tagsHeight = tags ? Math.round(tags.getBoundingClientRect().height) : 0;const sizeInMap = this.initialInputHeight || 40;// 这里input.style.height = this.selected.length === 0? sizeInMap + 'px': Math.max(tags ? (tagsHeight + (tagsHeight > sizeInMap ? 6 : 0)) : 0,sizeInMap) + 'px';if (this.visible && this.emptyText !== false) {this.broadcast('ElSelectDropdown', 'updatePopper');}});
},

这个方法在 disabled 变更时会触发:

watch: {selectDisabled() {this.$nextTick(() => {this.resetInputHeight();});},
...

我的解决办法

这个逻辑可能是为了多选时文本框的高度而设的,因为确认我的项目在单选时不需要考虑高度的变化,所以我直接替换这个监听回调,以解决了我的问题。

import Vue from 'vue'
import ElementUI from 'element-ui'const elSelectWatcherSelectDisabled = ElementUI.Select.watch.selectDisabled
ElementUI.Select.watch.selectDisabled = function () {if (this.multiple) {elSelectWatcherSelectDisabled()}
}Vue.use(ElementUI)
http://www.lryc.cn/news/400539.html

相关文章:

  • 写个网络爬虫
  • 模板方法模式的实现
  • Redis的计数功能
  • WPF学习(7) --MVVM模式
  • 【人工智能】-- 受限玻尔兹曼机
  • 在 Android 中定义和使用自定义属性
  • 【实战:python-Django发送邮件-短信-钉钉通知】
  • Todo List
  • 【Redis】Redis十大类型
  • 存储实验:Linux挂载iscsi硬盘与华为OceanStor创建LUN全流程
  • 高可用系统架构设计技术方案:Java架构师视角
  • C++ --> 类和对象(三)
  • JS【详解】类 class ( ES6 新增语法 )
  • vue中使用$set方法给对象添加属性
  • 【Python】ftplib的使用
  • CSS 【详解】CSS 函数(含 calc,min,max,clamp,cubic-bezier,env,steps 等)
  • 简单理解Lua 协程(coroutine)
  • (day18) leetcode 204.计数质数
  • SadTalker数字人服务器部署
  • Python实现一对多WebSocket发送给指定多个客户端
  • Power BI 工具介绍
  • 银河麒麟高级服务器操作系统V10加固操作指南
  • (leetcode学习)15. 三数之和
  • 算法训练 | 图论Part8 | 117. 软件构建、47. 参加科学大会
  • 编程从零基础到进阶(更新中)
  • MySQL运维实战之ProxySQL(9.6)SQL黑名单
  • 深入了解MySQL中的innodb_lock_wait_timeout
  • 102.qt qml-最全Table交互之多列固定、行列拖拽、自定义委托、标题交互使用教程
  • 文章管理小程序的设计
  • Ubuntu22.04安装NIVIDIA显卡驱动总结