前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >05-React Antd UI库

05-React Antd UI库

作者头像
彼岸舞
发布2022-08-24 08:40:27
9400
发布2022-08-24 08:40:27
举报

AntDesign UI 库

地址

代码语言:javascript
复制
https://ant.design/components

添加依赖

代码语言:javascript
复制
yarn add antd

我在使用的时候一致报错超时

代码语言:javascript
复制
npm install antd --save

可以使用NPM尝试

基础使用

引入组件

代码语言:javascript
复制
import {Button} from 'antd'

引入样式(一般全局引入一次)

代码语言:javascript
复制
import 'antd/dist/antd.css'

使用按钮

代码语言:javascript
复制
import React, {Component} from 'react';
import {Button} from 'antd'
import 'antd/dist/antd.css'

class App extends Component {
    render() {
        return (
            <div>
                <div>
                    <h2>Antd 的基本使用</h2>
                </div>
                <div>
                    <Button type='primary'>Antd button</Button>
                </div>
            </div>
        );
    }
}

export default App;

效果就是这样子喽

和官网的一模一样

其他的使用步骤一样, 参考样例, 其实其他的UI库的使用方式都一样, 没有啥好记得, 用的时候翻翻文档,就可以了

暴露配置

代码语言:javascript
复制
yarn eject
代码语言:javascript
复制
E:\js\react_antd>yarn eject
yarn run v1.22.19
$ react-scripts eject
NOTE: Create React App 2+ supports TypeScript, Sass, CSS Modules and more without ejecting: https://reactjs.org/blog/2018/10/01/create-react-app-v2.html

√ Are you sure you want to eject? This action is permanent. ... yes
Ejecting...

Copying files into E:\js\react_antd
  Adding \config\env.js to the project
  Adding \config\getHttpsConfig.js to the project
  Adding \config\modules.js to the project
  Adding \config\paths.js to the project
  Adding \config\webpack.config.js to the project
  Adding \config\webpackDevServer.config.js to the project
  Adding \config\jest\babelTransform.js to the project
  Adding \config\jest\cssTransform.js to the project
  Adding \config\jest\fileTransform.js to the project
  Adding \scripts\build.js to the project
  Adding \scripts\start.js to the project
  Adding \scripts\test.js to the project
  Adding \config\webpack\persistentCache\createEnvironmentHash.js to the project

Updating the dependencies
  Removing react-scripts from dependencies
  Adding @babel/core to dependencies
  Adding @pmmmwh/react-refresh-webpack-plugin to dependencies
  Adding @svgr/webpack to dependencies
  Adding babel-jest to dependencies
  Adding babel-loader to dependencies
  Adding babel-plugin-named-asset-import to dependencies
  Adding babel-preset-react-app to dependencies
  Adding bfj to dependencies
  Adding browserslist to dependencies
  Adding camelcase to dependencies
  Adding case-sensitive-paths-webpack-plugin to dependencies
  Adding css-loader to dependencies
  Adding css-minimizer-webpack-plugin to dependencies
  Adding dotenv to dependencies
  Adding dotenv-expand to dependencies
  Adding eslint to dependencies
  Adding eslint-config-react-app to dependencies
  Adding eslint-webpack-plugin to dependencies
  Adding file-loader to dependencies
  Adding fs-extra to dependencies
  Adding html-webpack-plugin to dependencies
  Adding identity-obj-proxy to dependencies
  Adding jest to dependencies
  Adding jest-resolve to dependencies
  Adding jest-watch-typeahead to dependencies
  Adding mini-css-extract-plugin to dependencies
  Adding postcss to dependencies
  Adding postcss-flexbugs-fixes to dependencies
  Adding postcss-loader to dependencies
  Adding postcss-normalize to dependencies
  Adding postcss-preset-env to dependencies
  Adding prompts to dependencies
  Adding react-app-polyfill to dependencies
  Adding react-dev-utils to dependencies
  Adding react-refresh to dependencies
  Adding resolve to dependencies
  Adding resolve-url-loader to dependencies
  Adding sass-loader to dependencies
  Adding semver to dependencies
  Adding source-map-loader to dependencies
  Adding style-loader to dependencies
  Adding tailwindcss to dependencies
  Adding terser-webpack-plugin to dependencies
  Adding webpack to dependencies
  Adding webpack-dev-server to dependencies
  Adding webpack-manifest-plugin to dependencies
  Adding workbox-webpack-plugin to dependencies

Updating the scripts
  Replacing "react-scripts start" with "node scripts/start.js"
  Replacing "react-scripts build" with "node scripts/build.js"
  Replacing "react-scripts test" with "node scripts/test.js"

Configuring package.json
  Adding Jest configuration
  Adding Babel preset

Running npm install...
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

up to date in 2s

207 packages are looking for funding
  run `npm fund` for details
Ejected successfully!

fatal: not a git repository (or any of the parent directories): .git
Staged ejected files for commit.

Please consider sharing why you ejected in this survey:
  http://goo.gl/forms/Bi6CZjk1EqsdelXk1

Done in 4.99s.

E:\js\react_antd>

多了脚手架的配置

按需导入Antd CSS样式

上面是直接引入全部的antd css样式, 但是有很多是用不到的, 所以需要按需引入, 虽然可以直击改默认的配置,但是不推荐, 容易把项目搞崩溃....

Antd使用craco按需加载样式与自定义主题

因为最新的版本工具已经改成了craco, 所以基于craco去做

代码语言:javascript
复制
yarn add @craco/craco babel-plugin-import craco-less

配置Package.json

代码语言:javascript
复制
/* package.json */
"scripts": {
-   "start": "react-scripts start",    //去除
-   "build": "react-scripts build", //去除
-   "test": "react-scripts test",  //去除
+   "start": "craco start",    //添加
+   "build": "craco build", //添加
+   "test": "craco test", //添加
}

在根目录下新增配置文件

craco.config.js

代码语言:javascript
复制
const CracoLessPlugin = require("craco-less");

module.exports = {
  //按需引入
  babel: {
    plugins: [["import",
               {
                 "libraryName": "antd",
                 "libraryDirectory": "es",
                 //可以设置为true即是less,注意!!!!此时不需要加引号
                 //也可以设置为css,css需要加引号
                 "style": true
               }
              ]]
  },
  //自定义主题
  plugins: [{
    plugin: CracoLessPlugin,
    options: {
      // 此处根据 less-loader 版本的不同会有不同的配置,详见 less-loader 官方文档
      lessLoaderOptions: {
        lessOptions: {
          //颜色即为自定义颜色
          modifyVars: {'@primary-color': 'blue'},
          javascriptEnabled: true
        }
      }
    }
  }]
}

重新启动项目即可

本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AntDesign UI 库
    • 地址
      • 添加依赖
        • 基础使用
          • 暴露配置
            • 按需导入Antd CSS样式
              • Antd使用craco按需加载样式与自定义主题
                • 配置Package.json
                  • 在根目录下新增配置文件
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                  http://www.vxiaotou.com