当前位置: 首页 > 工具软件 > postcss-less > 使用案例 >

vue cli3+项目使用postcss-px2rem或者postcss-plugin-px2rem适配

微生毅
2023-12-01

1. 使用postcss-px2rem或者postcss-plugin-px2rem讲vue项目的px全部转换未rem适配。

2. 下载:

npm install --save postcss-px2rem

npm install --save postcss-plugin-px2rem

3. 使用:在vue.config.js中

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? '/online/' : '/',
    // outputDir: 在npm run build时 生成文件的目录 type:string, default:'dist'
    // outputDir: 'dist',
    // pages:{ type:Object,Default:undfind }
    runtimeCompiler: true,
    devServer: {
        port: 8888, // 端口号
        host: 'localhost',
        https: false, // https:{type:Boolean}
        open: false, //配置自动启动浏览器
        //proxy: 'http://172.20.10.9:8080', // 配置跨域处理,只有一个代理
        proxy: {
            '/auidt': {
                target: 'http://172.20.10.9:8080',
                ws: true,
                changeOrigin: true
            },
        }, // 配置多个代理
    },
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    // require('postcss-px2rem')({  //配置postcss-px2rem适配
                    //     remUnit:100
                    // })
                    require('postcss-plugin-px2rem')({  //配置postcss-plugin-px2rem适配
                        rootValue: 100, //换算基数, 默认100  ,这样的话把根标签的字体规定为1rem为50px,这样就可以从设计稿上量出多少个px直接在代码中写多上px了。
                        unitPrecision: 5, //允许REM单位增长到的十进制数字。
                        //propWhiteList: [],  //默认值是一个空数组,这意味着禁用白名单并启用所有属性。
                        // propBlackList: [], //黑名单
                        exclude: /(node_module)/,  //默认false,可以(reg)利用正则表达式排除某些文件夹的方法,例如/(node_module)/ 。如果想把前端UI框架内的px也转换成rem,请把此属性设为默认值
                        // selectorBlackList: [], //要忽略并保留为px的选择器
                        // ignoreIdentifier: false,  //(boolean/string)忽略单个属性的方法,启用ignoreidentifier后,replace将自动设置为true。
                        // replace: true, // (布尔值)替换包含REM的规则,而不是添加回退。
                        mediaQuery: false,  //(布尔值)允许在媒体查询中转换px。
                        // minPixelValue: 3 //设置要替换的最小像素值(3px会被转rem)。 默认 0
                    }),
                ]
            }
        }
    }
}

4. 注意:

       4.1 如果页面这样配置页面全部被缩放很小。需要全局设置一个css:html,body的font-size为100px或者100PX。

html,body{
    font-size: 100PX;
}

      4.2 设置好了页面标签都需要设置宽高。或者设置display:flex。

 

 

 

 类似资料: