一、安装
yarn add postcss-px-to-viewport -D
由于可能include不生效,此时再安装
yarn add https://github.com/evrone/postcss-px-to-viewport --save-dev
二、配置
在根目录下,添加postcss.config.js文件,配置内容如下
- module.exports = {
- plugins: {
- 'postcss-px-to-viewport': {
- // 换成你本地在哪个分辨率下开发的尺寸
- viewportWidth: 1920,
- unitPrecision: 5, // 单位转换后保留的精度
- include: /\/src\/views\/project\/homepage\//, // 如果设置了include,那将只有匹配到的文件才会被转换,例如只转换 'src/mobile' 下的文件 (include: /\/src\/mobile\//)
- },
- }
- }
三、配置后重启项目,即可。
四、注意:对行内样式不友好,生效页面最好不要写行类样式