码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • React - Ant Design4.x版本安装使用,并按需引入和自定义主题


    React - Ant Design4.x版本安装使用,并按需引入和自定义主题

    • 一. 安装使用 antd
    • 二.antd 高级配置
      • 安装 craco,对 create-react-app 的默认配置进行自定义
      • 自定义主题
      • 安装 babel-plugin-import ,按需加载组件代码和样式

    Ant Design官网:https://4x.ant.design/docs/react/use-with-create-react-app

    一. 安装使用 antd

    1. 安装 antd
      yarn add antd@4.24.3

    2. 修改 src/App.css 文件,引入antd/dist/antd.css

      @import '~antd/dist/antd.css';
      
      • 1
    3. 修改 src/App.js 文件,引入 App.css 样式文件,并使用 andt 组件。

      import { Button } from 'antd';
      import './App.css';
      
      function App() {
        return (
          <div className="App">
            <Button type="primary">Button</Button>
          </div>
        );
      }
      
      export default App;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    4. 页面正常显示 antd 的蓝色按钮组件,说明已经把 antd 组件成功运行起来了。

    二.antd 高级配置

    安装 craco,对 create-react-app 的默认配置进行自定义

    1. 安装 craco
      yarn add @craco/craco

    2. 修改 package.json 里的 scripts 属性
      在这里插入图片描述

    3. 在项目根目录创建一个 craco.config.js 用于修改默认配置

      /* craco.config.js */
      module.exports = {
        // ...
      };
      
      • 1
      • 2
      • 3
      • 4

    自定义主题

    定制主题: https://4x.ant.design/docs/react/customize-theme-cn

    1. 安装 craco-less
      yarn add craco-less

    2. 修改craco.config.js 文件,配置 lessOptions

      const CracoLessPlugin = require("craco-less");
      
      module.exports = {
        plugins: [
          {
            plugin: CracoLessPlugin,
            options: {
              lessLoaderOptions: {
                lessOptions: {
                  // 自定义less变量
                  modifyVars: { "@primary-color": "#1DA57A" },
                  javascriptEnabled: true,
                },
              },
            },
          },
        ],
      };
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    3. src/App.css 文件修改为 src/App.less,并修改样式引用为 @import '~antd/dist/antd.less';
      在这里插入图片描述

    4. 修改 src/App.js,引用文件为 src/App.less
      在这里插入图片描述

    5. yarn start 重启项目访问页面,如果看到一个绿色的按钮就说明配置成功了。

    安装 babel-plugin-import ,按需加载组件代码和样式

    1. 安装 babel-plugin-import
      yarn add babel-plugin-import

    2. 修改craco.config.js 文件,配置 babel-plugin-import

      const CracoLessPlugin = require("craco-less");
      
      module.exports = {
        plugins: [
          {
            plugin: CracoLessPlugin,
            options: {
              lessLoaderOptions: {
                lessOptions: {
                  // 自定义less变量
                  modifyVars: { "@primary-color": "#1DA57A" },
                  javascriptEnabled: true,
                },
              },
            },
          },
        ],
        // 配置babel-plugin-import按需引用
        babel: {
          plugins: [
            ["import", { libraryName: "antd", libraryDirectory: "es", style: true }],
            ["@babel/plugin-proposal-decorators", { legacy: true }],
          ],
        },
      };
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
    3. 移除前面在 src/App.less 里全量添加的@import '~antd/dist/antd.less';,yarn start 重启项目访问页面,antd 组件的 js 和 css 代码都会按需加载。

  • 相关阅读:
    官宣!CNCF 正式接受 Istio 成为孵化项目
    5、JAVA入门——变量和数据类型1
    《架构风清扬-Java面试系列第25讲》聊聊ArrayBlockingQueue的特点及使用场景
    【老生谈算法】matlab实现遗传算法在调节控制系统参数中的应用——遗传算法
    数据结构学习系列之单向链表的清空与销毁
    Linux下安装Foldseek并从蛋白质的PDB结构中获取 3Di Token 和 3Di Embedding
    在Ubuntu系统中安装VNC并结合内网穿透实现公网远程访问
    【数学建模】MATLAB应用实战系列(八十八)-组合权重法应用案例(附MATLAB和Python代码)
    CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
    CesiumJS【Basic】- #024 加载webm文件(Primitive方式)
  • 原文地址:https://blog.csdn.net/Jie_1997/article/details/128032450
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号