1、 安装lib-flexible
npm i lib-flexible --save
2、在main.js引入lib-flexible
import 'lib-flexible/flexible'
3、安装postcss-pxtorem
npm install postcss-pxtorem -D
4、配置postcss-pxtorem
在根目录的postcss.config.js文件中修改,此文件自动生成,若没有,手动添加
module.exports = {
"plugins": {
"autoprefixer": {},
'postcss-pxtorem': {
rootValue: 75, // 75表示750设计稿,37.5表示375设计稿
propList: ['*']
}
}
}
5、对于引入vant组件的特殊处理
//vant设计稿为375,当以750设计稿为基础设置时,需要屏蔽vant
module.exports = {
"plugins": {
"autoprefixer": {},
'postcss-pxtorem': {
rootValue: 75, // 75表示750设计稿,37.5表示375设计稿
propList: ['*'],
selectorBlackList: ['van'] //过滤掉van不进行rem转换
}
}
}
6、针对ipad 和 ipad pro 设备无效,可以尝试设置
// 在index.html中添加如下代码
<script>
/(pad|pod|iPad|iPod|iOS)/i.test(navigator.userAgent)&&(head=document.getElementsByTagName('head'),viewport=document.createElement('meta'),viewport.name='viewport',viewport.content='target-densitydpi=device-dpi, width=480px, user-scalable=no',head.length>0&&head[head.length-1].appendChild(viewport));
</script>