1.安装lib-flexible(用于设置 rem 基准值)
npm i -S amfe-flexible
2.在main.js文件中引入lib-flexible
import ‘amfe-flexible’
3、安装postcss-pxtorem(postcss-pxtorem是一款 postcss 插件,用于将单位转化为 rem)
npm install postcss-pxtorem --save-dev
4、在public/index.html加入meta标签
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
5.vue.config.js添加:
const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');
module.exports = {
css: {
loaderOptions: {
postcss: {
plugins: [
autoprefixer(),
pxtorem({
rootValue: 75,
propList: ['*'],
"selectorBlackList":["van-"] //排除vant框架相关组件
})
]
}
}
}
};
6.在根目录下新建postcss.config.js,内容为:
const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');
module.exports = ({ file }) => {
let remUnit
if (file && file.dirname && file.dirname.indexOf('vant') > -1) {
remUnit = 37.5
} else {
remUnit = 75
}
return {
plugins: [
autoprefixer(),
pxtorem({
rootValue: remUnit,
propList: ['*'],
selectorBlackList: ['van-circle__layer']
})
]
}
}
注:动态设置rootValue的大小,即对于vant-ui框架的组件 将rootValue设置成37.5,对于我们自己的750宽度的设计稿的将rootValue设置成75
转:https://www.cnblogs.com/wuqun/p/12084304.html