前端学习

给create-react-app生成的react添加antd-

2020-09-04  本文已影响0人  小盐_814e

先将项目拆包

yarn eject

安装babel-plugin-import

npm install babel-plugin-import --save-dev

找到package.json中的babel部分
修改为

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

即可实现antd-mobile按需加载,修改引用方式

- import Button from 'antd-mobile/lib/button';
+ import { Button } from 'antd-mobile';

同样pc端安装antd懒加载

yarn add antd

安装babel-plugin-import

npm install babel-plugin-import --save-dev

找到package.json中的babel部分
修改为

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

猜你喜欢

热点阅读