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

本文介绍了如何在Vue CLI3项目中利用postcss-px2rem或postcss-plugin-px2rem将px转换为rem进行适配。详细步骤包括下载插件、配置vue.config.js以及注意需设置html,body的font-size和处理页面元素的宽高。
摘要由CSDN通过智能技术生成

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,
                changeOr
postcss-plugin-px2rem是一个PostCSS插件,用于将CSS中的px单位转换为rem单位,以实现适配不同屏幕尺寸的效果。cli3指的是Vue.js的脚手架工具,用于构建Vue项目的基础结构。PC和手机端的配置指的是在使用cli3构建Vue项目时,如何配置postcss-plugin-px2rem插件来处理PC和手机端的样式适配。 首先,需要在package.json文件中添加postcss-plugin-px2rem插件的依赖。 然后,在项目的根目录下,创建一个postcss.config.js的文件,用于配置postcss-plugin-px2rem插件的选项。 配置文件的内容如下: ```js module.exports = { plugins: [ require('postcss-plugin-px2rem')({ remUnit: 75, // rem单位的基准值 exclude: /node_modules/i, // 排除node_modules目录下的文件 mediaQuery: false, // 是否转换@media查询中的px minPixelValue: 2 // 小于指定数值的px不转换为rem }) ] } ``` 在配置文件中,我们设置了remUnit的值为75。这意味着在设计稿中,1px将被转换为0.01333rem(1/75=0.01333)。 我们还设置了exclude选项,用于排除node_modules目录下的文件,因为通常我们不希望对第三方库的样式进行转换。 mediaQuery选项用于确定是否转换@media查询中的px单位。 minPixelValue选项用于确定小于指定数值的px单位是否转换为rem。 最后,在cli3的配置文件vue.config.js中,添加postcss插件的配置,如下: ```js module.exports = { css: { loaderOptions: { postcss: { config: { path: 'postcss.config.js' } } } } } ``` 以上配置将使postcss-plugin-px2rem插件在构建Vue项目时自动应用于CSS文件。 通过以上的配置,我们就可以在cli3构建的Vue项目使用postcss-plugin-px2rem插件来实现PC和手机端的样式适配了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值