1.antd官网
https://ant.design/docs/react/introduce-cn
2、React中使用Antd
1)、安装antd
npm install antd --save / yarn add antd / cnpm install antd --save
2)、在react项目的css文件中引入Antd的css
@import '~antd/dist/antd.css';
3)、如何使用一个Button:
a、在对应的组件中引入Antd
import { Button } from 'antd';
b、在html本分写
<Button type="primary">Primary</Button>
3、react中使用Antd高级配置,按需引入css样式
现在组件已经成功运行起来了,但是在实际开发过程中还有很多问题,例如上面例子实际上市加载了全部的antd组件的样式(对前端性能是个隐患)。
1)、安装antd
npm install antd --save
2)、安装(react-app-rewired)一个对 create-react-app 进行自定义配置的社区解决方案
yarn add react-app-rewired / cnpm install react-app-rewired --save
3)、修改package.jsonz中react-scripts 需改为react-app-rewired
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
"eject": "react-app-rewired eject"
}
4)、在项目根目录创建一个 config-overrides.js 配置文件
const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
config = injectBabelPlugin(
['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }],
config,
);
return config;
};
5)、安装babel-plugin-import babel-plugin-import是一个用于按需加载组件代码和样式的 babel 插件
扫描二维码关注公众号,回复:
9063897 查看本文章
yarn add babel-plugin-import / cnpm install babel-plugin-import --save
6)、然后移除前面在 src/App.css 里全量添加的
@import '~antd/dist/antd.css';
直接引入组件使用就会有对应的css:
import { Button } from 'antd';
<Button type="primary">Primary</Button>