关于postcss-pxrem和vant - ui组件库冲突页面变小的问题

在Vue移动端项目中,使用postcss-px2vw进行px转rem自动化处理带来便利,但也导致引入的UI组件库样式受到影响。本文介绍了这一问题及其解决方法,通过配置postcss.config.js文件,实现组件库与postcss并行工作,避免样式冲突。

vue移动开发中,使用postcss-px2vw插件它会自动将px单位换算成rem,这样不用计算很方便,但同时也存在一些问题,如果我们需要使用ui组件库内容,它会将被引入组件的px转换为rem,最后影响样式。这个问题浪费了很长时间,在这总结一下以免忘了。。。

安装

npm install postcss-pxtorem --save

在postcss.config.js中写入:

const pxtorem = require('postcss-pxtorem')
const autoprefixer = require('autoprefixer')

module.exports = ({ file }) => {
  let rootValue
  if (file && file.dirname && file.dirname.indexOf('vant') > -1) {
    rootValue = 16
  } else {
    rootValue = 37.5
  }
  return {
    plugins: [
      autoprefixer(),
      pxtorem({
        rootValue: rootValue,
        propList: ['*'],
        minPixelValue: 2
      })
    ]
  }
}

这样ui组件库和postcss可以并行存在、不会互相影响。

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值