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

Ant Design Vue v4版本如何解决1px没有被postcss-px2rem转成rem的问题

背景说明

如果你的 Ant Design Vue 项目有要做适配的需求,那首先要选择一种适配方案。笔者选择的是用 postcss-px2rem 进行适配。笔者在配置了 postcss-px2rem的相关配置后,发现 postcss-px2rem 没有对 Ant Design Vue 进行适配。在网上看了一些文章之后,发现想对 Ant Design Vue 进行适配需要走它自己的规则:给 a-style-provider 组件传入 transformerspropsAnt Design Vue 官方自己提供了一套 transformers 方法,用这套方法可以对大于1px的单位进行转换,而不凑巧的是笔者的项目有对1px进行适配的需求,所以笔者必须解决这个问题。

解决方案

不直接用官方的方案,而是用官方方案的修改版。

编写一个px2rem的方法,然后在 a-style-provider 里引进去。

// 这应该是一个文件,例如px2Rem.ts
import unitless from '@emotion/unitless'
export interface Options {/*** The root font size.* @default 16*/rootValue?: number/*** The decimal numbers to allow the REM units to grow to.* @default 5*/precision?: number/*** Whether to allow px to be converted in media queries.* @default false*/mediaQuery?: boolean
}const pxRegex = /url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)px/gfunction toFixed(number: number, precision: number) {const multiplier = Math.pow(10, precision + 1),wholeNumber = Math.floor(number * multiplier)return (Math.round(wholeNumber / 10) * 10) / multiplier
}const transform = (options: Options = {}): Transformer => {const { rootValue = 16, precision = 5, mediaQuery = false } = optionsconst pxReplace = (m: string, $1: any) => {if (!$1) return mconst pixels = parseFloat($1)if (pixels < 1) return mconst fixedVal = toFixed(pixels / rootValue, precision)return `${fixedVal}rem`}const visit = (cssObj: any): any => {const clone: any = { ...cssObj }Object.entries(cssObj).forEach(([key, value]) => {if (typeof value === 'string' && value.includes('px')) {const newValue = value.replace(pxRegex, pxReplace)clone[key] = newValue}// no unitif (!unitless[key] && typeof value === 'number' && value !== 0) {clone[key] = `${value}px`.replace(pxRegex, pxReplace)}// Media queriesconst mergedKey = key.trim()if (mergedKey.startsWith('@') && mergedKey.includes('px') && mediaQuery) {const newKey = key.replace(pxRegex, pxReplace)clone[newKey] = clone[key]delete clone[key]}})return clone}return { visit } as Transformer<any, any>
}export default transform

App.vue该如下

import px2rem from "./px2Rem.ts" // 自行修改
<template><a-config-provider><a-style-provider :transformers="[px2rem]"><component /></a-style-provider></a-config-provider>
</template>
http://www.lryc.cn/news/499508.html

相关文章:

  • 武汉科技大学《2024年814自动控制原理真题》 (完整版)
  • 【锂电池实战】A123磷酸铁锂在线参数识别-一阶戴维南模型
  • Java多线程与线程池技术详解(四)
  • 树莓派开发笔记
  • 【数据结构】遍历二叉树
  • 嵌入式蓝桥杯学习7 产生PWM
  • 档案学实物
  • 数据清洗代码:缺失值,异常值,离群值Matlab处理
  • Windows设备go环境安装配置
  • 导体、半导体和绝缘体
  • shell 6 if条件判断与for循环结构 (泷羽sec)
  • MetaGPT 安装
  • 论文阅读:Single-cell transcriptomics of 20 mouse organs creates a Tabula Muris
  • 图生3d 图生全景 学习笔记
  • 分库分表—4.数据迁移系统文档
  • HAMR技术进入云存储市场!
  • Vulnhub---kioptirx5 超详细wp
  • 单片机状态机实现多个按键同时检测单击、多击、长按等操作
  • oracle之用户的相关操作
  • 黑马redis
  • HCIA-Access V2.5_1_2 PON技术的特点、优势与典型应用
  • css部分
  • 【TCP 网络通信(发送端 + 接收端)实例 —— Python】
  • LSTM+改进的itransformer时间序列预测模型代码
  • Apache-HertzBeat 开源监控默认口令登录
  • Delete Number
  • Linux常用快捷键
  • 针对xpath局限的解决方案
  • 深入解析 HTML Input 元素:构建交互性表单的核心
  • ffmpeg转码与加水印