首页 / REACT / react引入antd组件
react引入antd组件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了react引入antd组件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1463字,纯文字阅读大概需要3分钟。
内容图文
![react引入antd组件](/upload/InfoBanner/zyjiaocheng/1142/59ebc589f70b498481bf982fec22b5e6.jpg)
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>
原文:https://www.cnblogs.com/duocaishenghuo/p/13900627.html
内容总结
以上是互联网集市为您收集整理的react引入antd组件全部内容,希望文章能够帮你解决react引入antd组件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。