1.安装插件
(1)安装px2rem-loader(devDependencies)
npm install px2rem-loader --save-dev
(2)移动端适配解决npm包 "lib-flexible" (dependencies)
npm install lib-flexible --save
(3)main.js中引入 "lib-flexible"
import 'lib-flexible' // 移动端适配 (目录: hello-world/src/main.js)
2、在项目文件夹下创建 "vue.config.js"文件 里面写入
module.exports = {
chainWebpack: config => {
config.module
.rule('css')
.test(/\.css$/)
.oneOf('vue')
.resourceQuery(/\?vue/)
.use('px2rem')
.loader('px2rem-loader')
.options({
remUnit: 48
})
},
lintOnSave: true,
css: {
loaderOptions: {
postcss: {
plugins: [
require('postcss-plugin-px2rem')({
rootValue: 48, //换算基数, 默认100 ,这样的话把根标签的字体规定为1rem为50px,这样就可以从设计稿上量出多少个px直接在代码中写多上px了。
// unitPrecision: 5, //允许REM单位增长到的十进制数字。
//propWhiteList: [], //默认值是一个空

本文介绍了在Vue CLI项目中如何使用px2rem-loader和postcss-plugin-px2rem进行px到rem的转换,以适应移动端适配。首先通过npm安装px2rem-loader和lib-flexible,然后在main.js中引入lib-flexible。接着,创建vue.config.js文件进行配置。由于px2rem-loader仅支持css,对于less、stylus等其他预处理器,需要安装postcss-plugin-px2rem。最后,设置remUnit和rootValue的值以匹配设计图尺寸。
最低0.47元/天 解锁文章
3264

被折叠的 条评论
为什么被折叠?



