当前位置: 首页 > news >正文

从零搭建React框架--第一章:create-react-app、antd、less

目录

引言
第一章:项目创建、antd、less
第二章:路由拦截、路由嵌套
第三章:状态管理 Redux
第四章:网络请求、代理、mockjs
第五章:webpack配置
第六章:Eslint

源码地址

https://github.com/dxn920128/cms-base

创建项目

本次项目使用create-react-app命令进行创建

// npm 全局安装create-react-app
npm install -g create-react-app
// 创建项目
npx create-react-app react-demo --typescript
// yarn
yarn create react-app react-demo --template typescript

webpack配置

使用 create-react-app初始化项目后,需要对webpack进行配置。webpack配置包含:less进行antd主题配置、别名、请求代理、mocker数据等等。
1、使用 npm run eject 命令将所有内建的配置暴露出来。
2、本次推荐使用 craco 进行配置。

  • @craco/craco
  • craco-less
  • babel-plugin-import 组件按需加载
  • antd react优秀的前端ui库
npm install -D @craco/craco  @babel/plugin-proposal-decorators babel-plugin-import craco-less 
npm install antd

1、修改package.json启动项。

 "scripts": {"start": "craco start","build": "craco build","test": "craco test"},

2、创建craco.config.js并进行配置

const CracoLessPlugin = require('craco-less')
module.exports = {plugins: [{plugin: CracoLessPlugin,//引入lessoptions: {lessLoaderOptions: {lessOptions: {javascriptEnabled: true //js修改主题样式}}}}],babel: {plugins: [['@babel/plugin-proposal-decorators', { legacy: true }], //装饰器['import',//按需引入antd 样式{'libraryName': 'antd','libraryDirectory': 'es','style': true}]]},
};

3、定义antd全局样式,在创建 index.less文件

src/assets/style/index.less

@import "~antd/dist/antd.less";@primary-color: #1890ff; //主题色
@border-radius-base: 2px;
@link-color: #1890ff; // 链接色
@success-color: #52c41a; // 成功色
@warning-color: #faad14; // 警告色
@error-color: #f5222d; // 错误色
@font-size-base: 14px; // 主字号
@heading-color: rgba(0, 0, 0, 0.85); // 标题色
@text-color: rgba(0, 0, 0, 0.65); // 主文本色
@text-color-secondary: rgba(0, 0, 0, 0.45); // 次文本色
@disabled-color: rgba(0, 0, 0, 0.25); // 失效色
@border-radius-base: 2px; // 组件/浮层圆角
@border-color-base: #d9d9d9; // 边框色
@layout-header-background: #ffffff;
@layout-body-background: #ffffff;@box-shadow-base: 0 3px 6px -4px rgba(0, 0, 0, 0.12),0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 9px 28px 8px rgba(0, 0, 0, 0.05); // 浮层阴影

4、全局引入index.less

import React from 'react'
import ReactDOM from 'react-dom'
import reportWebVitals from './reportWebVitals'
import { Provider } from 'react-redux'
import store from './store/index'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/lib/locale/zh_CN'
import AppRouter from './pages/frame/appRouter'
import './assets/style/index.less'ReactDOM.render(<ConfigProvider locale={zhCN}><Provider store={store}><AppRouter /></Provider></ConfigProvider>,document.getElementById('root')
)
最后编辑于:2025-06-15 11:02:55


喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.dtcms.com/a/317866.html

相关文章:

  • LAS平台Vibe Data Processing:AI驱动的数据处理新范式
  • Chrontel昆泰-【CH7036A-BF】CH7036 LVDS to HDMI/VGA/LVDS Converter
  • 基于MATLAB实现的具有螺旋相位板的4F系统用于图像边缘增强的仿真
  • 软件定义汽车 --- 电子电气架构的驱动
  • 在ubuntu上使用jenkins部署.net8程序
  • 【概念学习】早期神经网络
  • Redis 缓存三大核心问题:穿透、击穿与雪崩的深度解析
  • [AI 生成] hive 面试题
  • Document Object Model
  • 机器学习-LinearRegression
  • harbor仓库搭建(配置https)
  • MCU程序的编译与链接及格式转换
  • 防御保护防火墙简单实验报告
  • Git 乱码文件处理全流程指南:从识别到彻底清除
  • MySQL的约束条件:
  • 【Linux】调试器gdb/cgdb的使用
  • 生成式 AI 重塑自动驾驶仿真:4D 场景生成技术的突破与实践
  • vector使用模拟实现
  • 牛客AI简历筛选:破解秋招效率难题
  • 向量数据库基础入门:RAG 与向量检索基础认知构建
  • 《C语言程序设计》笔记p9
  • LLM——浅谈 LangGraph 中断式工作流:构建一个可交互的问答流程
  • 4、docker数据卷管理命令 | docker volume
  • 【关于Java的对象】
  • vue3 el-dialog自定义实现拖拽、限制视口范围增加了拖拽位置持久化的功能
  • 实战教程:从“对象文件为空“到仓库重生——修复 Git 仓库损坏全记录
  • 大数据存储域——Hive数据仓库工具
  • STM32 APP跳转后无法进入中断
  • QT----不同线程中信号发送了槽函数没反应QObject::connect: Cannot queue arguments of typeXXX
  • C++编程语言:标准库:工具类(Bjarne Stroustrup)