方案1:使用vant插件lib-flexible 设置rem基准值 第一步 安装 amfe-flexible 指令:npm i amfe-flexible 第二步 在main.js import 'amfe-flexible' (源码分析待更新)
方案2: 使用vant插件 viewport-to-rem 第一步 安装postcss-pxtorem
npm install postcss-pxtorem -D
第二步 配置postcss-pxtorem,在根目录的.postcssrc.js文件中修改 此文件自动生成,若没有,手动添加
module.exports = {
"plugins": {
"autoprefixer": {},
'postcss-pxtorem': {
rootValue: 75, // 75表示750设计稿,37.5表示375设计稿
propList: ['*']
}
}
}
对于引入vant组件的特殊处理
- vant设计稿为375,当以750设计稿为基础设置时,需要屏蔽vant
在postcss-pxtorem对象中添加
selectorBlackList: ['van']
- 忽略文件
在postcss-pxtorem对象中添加
exclude: /web/i //忽略 web下所有文件
针对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>
[部分转载于](vue-移动端适配-postcss-pxtorem - 简书 (jianshu.com)) 。