码农知识堂 - 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 代码都会按需加载。

  • 相关阅读:
    GoWeb——Gin框架入门(第一章)
    Ubuntu20.04 如何开启root账户登陆
    Linux学习记录1:启程
    FullCalendarDemo5 控件的实例讲解—拖拽实现值班排班(一)
    正则表达式新解
    【shell脚本编写】判断条件中 -z到-d 的使用
    几个非常有意思的javascript API
    微机原理与接口技术复习题
    DS森林叶子编码/森林转二叉树 【数据结构】
    软件测试和软件开发之间的关系及模型
  • 原文地址:https://blog.csdn.net/Jie_1997/article/details/128032450
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号