1、npm add postcss-px2rem
2、src下创建/config/rem.js
// 基准大小
const baseSize = 16
// 设置 rem 函数
function setRem() {
// 当前页面宽度相对于 750 宽的缩放比例,可根据自己需要修改。
const scale = document.documentElement.clientWidth / 1920
// 设置页面根节点字体大小
document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置 rem
window.onresize = function() {
setRem()
}
3、mian.js 中引入代码
import './config/rem' //px转rem
4、vue.config.js中
const px2rem = require('postcss-px2rem')
const postcss = px2rem({
remUnit: 16 //基准大小 baseSize,需要和rem.js中相同
})
css: {
loaderOptions: {
postcss: {
plugins: [
postcss
]
}
}
},