react的create-react-app移动端rem适配

本文介绍了使用create-react-app创建的React项目中,如何进行移动端的rem适配。详细步骤包括安装相关库、在index.js引入lib-flexible以及修改webpack.config.js的配置,解决了过程中可能遇到的错误和版本问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 安装:

npm install lib-flexible postcss-pxtorem -S 

2. 引入

在项目入口文件index.js引入lib-flexible

import 'lib-flexible';

3. 修改webpack.config.js

3.1 初始化配置

npm run eject

这时候可能会报错

解决方案

git init

git add .

git commit -m 'init'

3.2. 修改webpack.config.js

先引入:

const pxtorem = require('postcss-pxtorem')

 然后找到postcss-loader 替换一下,注释的部分加入

{
  loader: require.resolve('postcss-loader'),
  options: {
    ident: 'postcss',
    plugins: () => [
      require('postcss-flexbugs-fixes'),
      require('postcss-preset-env')({
        autoprefixer: {
          flexbox: 'no-2009',
        },
        stage: 3,
      }),
      postcssNormalize(),
      // 新增加---------------------------------------------
      pxtorem({
        rootValue: 37.5, 
        propWhiteList: [],
        minPixelValue: 2,
        exclude: /node_modules/i
      })
      //--------------------------------------------------
    ],
    sourceMap: isEnvProduction && shouldUseSourceMap,
  },
},

这时候会报错可能是版本过高

解决思路:

https://www.cnblogs.com/liangziaha/p/14492288.html

解决方案:

npm i postcss-pxtorem@5.1.1

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值