无需引入过多的css,不需要加载全局的css

1.手动引入

  1. import Button from 'antd/es/button'
  2. import 'antd/es/button/style/css'

2.通过配置

1.拉取webpack的配置

  1. npm run eject

文件被修改了无法使用npm run eject命令
image.png
解决方式删除.git文件夹
安装完成多出一个config文件夹
image.png
2.安装.babel文件

  1. yarn add babel-plugin-import --save-dev

3.重新yarn start运行项目

4.修改根目录的package.json的babel属性,配置plugins
image.png

  1. "babel": {
  2. "presets": [
  3. "react-app"
  4. ],
  5. "plugins":[
  6. [
  7. "import",
  8. {
  9. "libraryName":"antd",
  10. "libraryDirectory":"es",
  11. "style":"css"
  12. }
  13. ]
  14. ]
  15. }

5.重启项目,使用

  1. import { Button } from 'antd';
  2. // import Button from 'antd/es/button'
  3. // import 'antd/es/button/style/css'
  4. // import 'antd/dist/antd.css'; // or 'antd/dist/antd.less'
  5. function App() {
  6. return (
  7. <div className="App">
  8. <Button type="primary">Primary Button</Button>
  9. <Button>Default Button</Button>
  10. <Button type="dashed">Dashed Button</Button>
  11. <br />
  12. <Button type="text">Text Button</Button>
  13. <Button type="link">Link Button</Button>
  14. </div>
  15. );
  16. }
  17. export default App;