React - create-react-app

罗睿识
2023-12-01

使用yarn作为包管理工具,安装yarn

npm i yarn -g

安装项目所需的第三方包(使用antd要安装less与less-loader)

yarn add react-router-dom axios less-loader less antd

需要在webpack中配置less,在项目文件中暴露webpack

yarn eject

运行yarn eject可能出现问题,参考:npm run eject 报错This git repository has untracked files or uncommitted changes解决方案

less配置,参考:REACT配置LESS

其他问题,例如以下错误信息(提示缺少什么包,直接安装就好)

./node_modules/react-dev-utils/webpackHotDevClient.js
Error: [BABEL] G:\_myCode\react\my-app\node_modules\react-dev-utils\webpackHotDevClient.js: Cannot find 
module '@babel/helper-create-regexp-features-plugin' 

引入antd(注意要引入antd.css文件)

import { Button } from 'antd'
import 'antd/dist/antd.css'

安装yarn add babel-plugin-import(按需引入css文件)

yarn add babel-plugin-import

修改package.json文件,参考:React使用babel-plugin-import的问题

"babel": {
  "presets": [
    "react-app"
  ],
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "libraryDirectory": "es",
        "style": "css"
      }
    ]
  ]
}

 

 类似资料: