1.安装两个插件
npm i postcss-pxtorem -S
npm i amfe-flexible -S
2.根据vue3提供的新配置方法,在vue.config.js中对CSS做配置:
const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');
module.exports = {
css:{
sourceMap:false,
loaderOptions: {
postcss: {
plugins: [
autoprefixer(),
pxtorem({
rootValue: 37.5,
propList: ['*']
})
]
}
}
}
}
3. rootValue 75.0的值其实就是代表ui设计稿的750px的像素,引用的时候css直接写宽750px就相当于100%,它会自动根据屏幕进行计算成rem,无需对原测量值进行REM的换算(这里推荐37.5,结合2x的设计图开发,因为vant的组件你会发现37.5更为适合)。
4.在main.js文件夹下引入
import 'amfe-flexible';