首页 > 其他分享 >element ui踩坑

element ui踩坑

时间:2023-09-27 11:37:15浏览次数:30  
标签:el style element let domContent 组件 ui

项目适配需要rem转换 ,但UI组件内部未转换

问题:由于组件内 ,某些组件宽度和高度是通过prop传参,然后对行内样式动态赋值,所以单位还是px
网上找的方法:
element uigithub源码拉下来,然后修改组件源码,然后打包,然后打补丁替换lib文件夹。

个人觉得太繁琐,问题在于,所有的组件都得适配,那不得所有的组件都得进行源码修改

思路:
1.将传参加上单位 rem
2.将组件改为按需引入,在注册之前改写,或者监听,将已赋值的行内样式,单位转为rem
main.js中引入

//主动引入后会将element ui 内部css转为rem
import "element-ui/lib/theme-chalk/index.css";

//rem转换
function pxToRem(_s, fontSize = 1920 / 20) {
  //匹配:20px或: 20px不区分大小写
  var reg = /(\:|: )+(\d)+(px)/gi;
  let newStr = _s.replace(reg, function (_x) {
    _x = _x.replace(/(\:|: )/, "").replace(/px/i, "");
    return ":" + parseFloat(_x) / fontSize + "rem";
  });
  return newStr;
}
// element组件
import { Table } from "element-ui";
Vue.component("ElTable", {
  ...Table,
  watch: {
    ...Table.watch,
	//参考el-table源码可知,列表组件尺寸变化时触发
    bodyHeight: {
      immediate: true,
      handler(value) {
        //单位换算
        this.$nextTick(() => {
          let domContent = app.querySelectorAll("*[style]");
          Array.prototype.slice.call(domContent, 0);
          domContent.forEach((el) => {
            if (el.classList.value.includes("el-")) {
              let cloneDom = el.cloneNode();
              let style = pxToRem(cloneDom.getAttribute("style"));
              el.setAttribute("style", style);
            }
          });
        });
      },
      deep: true,
    },
    data: {
      immediate: true,
      handler(value) {
        this.store.commit("setData", value);
        //单位换算
        this.$nextTick(() => {
          let domContent = app.querySelectorAll("*[style]");
          Array.prototype.slice.call(domContent, 0);
          // let htmlDom = document.getElementsByTagName("html")[0];
          domContent.forEach((el) => {
            if (el.classList.value.includes("el-")) {
              let cloneDom = el.cloneNode();
              let style = pxToRem(cloneDom.getAttribute("style"));
              el.setAttribute("style", style);
            }
          });
        });
      },
    },
  },
});

标签:el,style,element,let,domContent,组件,ui
From: https://www.cnblogs.com/wxyblog/p/17732225.html

相关文章

  • uni-ui组件使用
    由于uni-app独特的easycom技术,可以免引用、注册,直接使用各种符合规则的vue组件。如果你没有创建uni-ui项目模板,也可以在你的工程里,通过uni_modules单独安装需要的某个组件比如找到uni-icon的下载:https://ext.dcloud.net.cn/plugin?name=uni-icons右侧点使用HBuilderX导入......
  • OpenHarmony装饰指定自定义组件:@BuilderParam装饰器
     当开发者创建了自定义组件,并想对该组件添加特定功能时,例如在自定义组件中添加一个点击跳转操作。若直接在组件内嵌入事件方法,将会导致所有引入该自定义组件的地方均增加了该功能。为解决此问题,ArkUI引入了@BuilderParam装饰器,@BuilderParam用来装饰指向@Builder方法的变量,开......
  • element-ui plus 修改对话框的样式,无效
    <el-dialogv-model="dialogVisible"title="Tips"width="30%":before-close="handleClose"append-to-body><span>Thisisamessage</span><template#footer>......
  • lombok注解:@Builder
    带有注释的方法@Builder(从现在起称为target)会导致生成以下7个内容:名为的内部静态类FooBuilder,具有与静态方法相同的类型参数(称为builder)。在构建器中:目标的每个参数都有一个私有非静态非最终字段。在构建器中:包私有无参数空构造函数。在构建器中:目标的每个参数都有一个类......
  • Vue3 引入 Element Plus
    ElementPlus简介ElementPlus是一个基于Vue3的UI组件库,其设计原则可分为一致(Consistency)、反馈(Feedback)、效率(Efficiency)、可控(Controllability)四个方面。目前ElementPlus可使用的UI组件种类丰富,除了按钮、边框、icon等基本组件,还可以在项目中引入表单组件、数据展示组件、导航......
  • 一些常见小程序的UI设计分享
    外卖优惠券小程序的UI设计电子商城系统UI分享A B  C ......
  • element ui 的picker-option 30天前限制和30天后限制
    pickerOptionsStart:{disabledDate:(time)=>{//获取当前日期并减少30天//console.log(time,'..........tiem');//console.log(time,'..........tiem');if(this.endDate!=''){//co......
  • ERROR: Could not find a version that satisfies the requirement selunium (from ve
    错误信息ERROR:Couldnotfindaversionthatsatisfiestherequirementselenium(fromversions:none)ERROR:Nomatchingdistributionfoundforselenium解决方案方法1:增大超时时间pip--default-timeout=100installselenium方法2:修改安装源为清华安装源pipi......
  • UGUI 优化
    UI更新Canvas.SendWillRenderCanvases--UI更新耗时color 颜色normal 法线position 顶点位置包括uisizeAnchorsPivot(缩放平移旋转不影响)tangent 切线uv0 网格第一个纹理坐标uv1.....替换图片文本优化减少图片切换减少颜色变化顶点位......
  • NanUI网络拦截
    publicclassMyResponseFilter:CefResponseFilter{privateMemoryStreamoutputStream=newMemoryStream();///<summary>//////</summary>///<paramname="dataIn">数据输入</param&g......