vue-cli3.0使用amfe-flexible + postcss-pxtorem,适配移动端并px自动转rem

4,353 阅读1分钟

记录一下 移动端适配方案 amfe-flexible

先初始化vue-cli3项目

第一步:下载 postcss-pxtorem(npm i postcss-pxtorem -D)

第二步:下载 amfe-flexible(npm i amfe-flexible -S)

第三步:在main.js中引入amfe-flexible

import 'amfe-flexible/index'

第四步:新建vue.config.js,配置postcss-pxtorem

module.exports = {
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    require('postcss-pxtorem')({ // 把px单位换算成rem单位
                        rootValue: 75, // 换算的基数(设计图750的根字体为75)
                        // selectorBlackList: ['weui', 'mu'], // 忽略转换正则匹配项
                        propList: ['*']
                    })
                ]
            }
        }
    },
    configureWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            // 为生产环境修改配置...
        } else {
            // 为开发环境修改配置...
        }
    }
}

第五步:重启项目,就可以看到样式px转为rem