react引入antd組件


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: 

		1、在對應的組件中引入Antd        import { Button } from 'antd';


		2、<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.json

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、安裝babel-plugin-import   babel-plugin-import是一個用於按需加載組件代碼和樣式的 babel 插件

	yarn add babel-plugin-import   /  cnpm install babel-plugin-import --save


5、在項目根目錄創建一個 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;
 };



7、然后移除前面在 src/App.css 里全量添加的 @import '~antd/dist/antd.css'; 直接引入組件使用就會有對應的css
	
	

	import { Button } from 'antd';


	<Button type="primary">Primary</Button>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM