• 05-React Antd UI库


    AntDesign UI 库

    地址

    https://ant.design/components
    

    添加依赖

    yarn add antd
    

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

    npm install antd --save
    

    可以使用NPM尝试

    基础使用

    引入组件

    import {Button} from 'antd'
    

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

    import 'antd/dist/antd.css'
    

    使用按钮

    1. import React, {Component} from 'react';
    2. import {Button} from 'antd'
    3. import 'antd/dist/antd.css'
    4. class App extends Component {
    5. render() {
    6. return (
    7. <div>
    8. <div>
    9. <h2>Antd 的基本使用</h2>
    10. </div>
    11. <div>
    12. <Button type='primary'>Antd button</Button>
    13. </div>
    14. </div>
    15. );
    16. }
    17. }
    18. export default App;

    效果就是这样子喽

    和官网的一模一样

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

    暴露配置

    yarn eject
    
    1. E:\js\react_antd>yarn eject
    2. yarn run v1.22.19
    3. $ react-scripts eject
    4. 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
    5. Are you sure you want to eject? This action is permanent. ... yes
    6. Ejecting...
    7. Copying files into E:\js\react_antd
    8. Adding \config\env.js to the project
    9. Adding \config\getHttpsConfig.js to the project
    10. Adding \config\modules.js to the project
    11. Adding \config\paths.js to the project
    12. Adding \config\webpack.config.js to the project
    13. Adding \config\webpackDevServer.config.js to the project
    14. Adding \config\jest\babelTransform.js to the project
    15. Adding \config\jest\cssTransform.js to the project
    16. Adding \config\jest\fileTransform.js to the project
    17. Adding \scripts\build.js to the project
    18. Adding \scripts\start.js to the project
    19. Adding \scripts\test.js to the project
    20. Adding \config\webpack\persistentCache\createEnvironmentHash.js to the project
    21. Updating the dependencies
    22. Removing react-scripts from dependencies
    23. Adding @babel/core to dependencies
    24. Adding @pmmmwh/react-refresh-webpack-plugin to dependencies
    25. Adding @svgr/webpack to dependencies
    26. Adding babel-jest to dependencies
    27. Adding babel-loader to dependencies
    28. Adding babel-plugin-named-asset-import to dependencies
    29. Adding babel-preset-react-app to dependencies
    30. Adding bfj to dependencies
    31. Adding browserslist to dependencies
    32. Adding camelcase to dependencies
    33. Adding case-sensitive-paths-webpack-plugin to dependencies
    34. Adding css-loader to dependencies
    35. Adding css-minimizer-webpack-plugin to dependencies
    36. Adding dotenv to dependencies
    37. Adding dotenv-expand to dependencies
    38. Adding eslint to dependencies
    39. Adding eslint-config-react-app to dependencies
    40. Adding eslint-webpack-plugin to dependencies
    41. Adding file-loader to dependencies
    42. Adding fs-extra to dependencies
    43. Adding html-webpack-plugin to dependencies
    44. Adding identity-obj-proxy to dependencies
    45. Adding jest to dependencies
    46. Adding jest-resolve to dependencies
    47. Adding jest-watch-typeahead to dependencies
    48. Adding mini-css-extract-plugin to dependencies
    49. Adding postcss to dependencies
    50. Adding postcss-flexbugs-fixes to dependencies
    51. Adding postcss-loader to dependencies
    52. Adding postcss-normalize to dependencies
    53. Adding postcss-preset-env to dependencies
    54. Adding prompts to dependencies
    55. Adding react-app-polyfill to dependencies
    56. Adding react-dev-utils to dependencies
    57. Adding react-refresh to dependencies
    58. Adding resolve to dependencies
    59. Adding resolve-url-loader to dependencies
    60. Adding sass-loader to dependencies
    61. Adding semver to dependencies
    62. Adding source-map-loader to dependencies
    63. Adding style-loader to dependencies
    64. Adding tailwindcss to dependencies
    65. Adding terser-webpack-plugin to dependencies
    66. Adding webpack to dependencies
    67. Adding webpack-dev-server to dependencies
    68. Adding webpack-manifest-plugin to dependencies
    69. Adding workbox-webpack-plugin to dependencies
    70. Updating the scripts
    71. Replacing "react-scripts start" with "node scripts/start.js"
    72. Replacing "react-scripts build" with "node scripts/build.js"
    73. Replacing "react-scripts test" with "node scripts/test.js"
    74. Configuring package.json
    75. Adding Jest configuration
    76. Adding Babel preset
    77. Running npm install...
    78. npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
    79. up to date in 2s
    80. 207 packages are looking for funding
    81. run `npm fund` for details
    82. Ejected successfully!
    83. fatal: not a git repository (or any of the parent directories): .git
    84. Staged ejected files for commit.
    85. Please consider sharing why you ejected in this survey:
    86. http://goo.gl/forms/Bi6CZjk1EqsdelXk1
    87. Done in 4.99s.
    88. E:\js\react_antd>

    多了脚手架的配置

    按需导入Antd CSS样式

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

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

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

    yarn add @craco/craco babel-plugin-import craco-less
    

    配置Package.json

    1. /* package.json */
    2. "scripts": {
    3. - "start": "react-scripts start", //去除
    4. - "build": "react-scripts build", //去除
    5. - "test": "react-scripts test", //去除
    6. + "start": "craco start", //添加
    7. + "build": "craco build", //添加
    8. + "test": "craco test", //添加
    9. }

    在根目录下新增配置文件

    craco.config.js

    1. const CracoLessPlugin = require("craco-less");
    2. module.exports = {
    3. //按需引入
    4. babel: {
    5. plugins: [["import",
    6. {
    7. "libraryName": "antd",
    8. "libraryDirectory": "es",
    9. //可以设置为true即是less,注意!!!!此时不需要加引号
    10. //也可以设置为css,css需要加引号
    11. "style": true
    12. }
    13. ]]
    14. },
    15. //自定义主题
    16. plugins: [{
    17. plugin: CracoLessPlugin,
    18. options: {
    19. // 此处根据 less-loader 版本的不同会有不同的配置,详见 less-loader 官方文档
    20. lessLoaderOptions: {
    21. lessOptions: {
    22. //颜色即为自定义颜色
    23. modifyVars: {'@primary-color': 'blue'},
    24. javascriptEnabled: true
    25. }
    26. }
    27. }
    28. }]
    29. }

    重新启动项目即可

  • 相关阅读:
    【Python计算机视觉】Python全栈体系(二十四)
    多维时序 | MATLAB实现WOA-CNN-GRU-Attention多变量时间序列预测(SE注意力机制)
    Unity —— 复建(day1)
    【jvm如何在设计层面实现线程实现停顿时间控制的思考】
    C++&QT-day5
    用于命名实体识别的模块化交互网络
    Excel自定义排序和求和
    Algorithm Review 8 分治
    HTML5期末大作业【红色的电影售票平台网站】web前端 html+css+javascript网页设计实例 企业网站制作
    java计算机毕业设计bs架构实习管理系统源码+mysql数据库+系统+lw文档+部署
  • 原文地址:https://blog.csdn.net/flowerStream/article/details/126493660