一、前言
Vue CLI 内部使用了 PostCSS。并且默认开启了 autoprefixer。
二、使用 postcss-pxtorem
1. 安装 postcss-pxtorem
$ npm install postcss-pxtorem
2. 项目根目录下创建 postcss.config.js 文件并写入如下内容
// postcss.config.jsmodule.exports = {plugins: {autoprefixer: {// 不需要设置 browsers 属性了,它会自动去读取 .browserslistrc 配置文件// browsers: ['Android >= 4.0', 'iOS >= 8'],},'postcss-pxtorem': {// 转换的基准值 1rem = 37.5px// xrem = 44pxrootValue: 37.5,propList: ['*']}}}
3. 重新运行项目(npm run serve)
就能看到 px 自动转换成了 rem
PS: 如果希望某个样式的 px 不进行转换的化,将 px 写成 大写的 PX 即可
4. 使用 amfe-flexible 动态修改根元素的 font-size 大小
1. 下载并保存到项目的 public/lib 目录中 下载链接
2. public/index.html 中引入(推荐在 head 部分引入)
<!DOCTYPE html><html lang="en"><head>...<script src="./lib/flexible.js"></script></head><body><div id="app"></div><!-- built files will be auto injected --></body></html>
