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

目錄

引言
第一章:項(xiàng)目創(chuàng)建、antd、less
第二章:路由攔截、路由嵌套
第三章:狀態(tài)管理 Redux
第四章:網(wǎng)絡(luò)請(qǐng)求、代理、mockjs
第五章:webpack配置
第六章:Eslint

源碼地址

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

創(chuàng)建項(xiàng)目

本次項(xiàng)目使用create-react-app命令進(jìn)行創(chuàng)建

// npm 全局安裝create-react-app
npm install -g create-react-app
// 創(chuàng)建項(xiàng)目
npx create-react-app react-demo --typescript
// yarn
yarn create react-app react-demo --template typescript

webpack配置

使用 create-react-app初始化項(xiàng)目后,需要對(duì)webpack進(jìn)行配置。webpack配置包含:less進(jìn)行antd主題配置、別名、請(qǐng)求代理、mocker數(shù)據(jù)等等。
1、使用 npm run eject 命令將所有內(nèi)建的配置暴露出來(lái)。
2、本次推薦使用 craco 進(jìn)行配置。

npm install -D @craco/craco  @babel/plugin-proposal-decorators babel-plugin-import craco-less 
npm install antd

1、修改package.json啟動(dòng)項(xiàng)。

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

2、創(chuàng)建craco.config.js并進(jìn)行配置

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

3、定義antd全局樣式,在創(chuàng)建 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; // 錯(cuò)誤色
@font-size-base: 14px; // 主字號(hào)
@heading-color: rgba(0, 0, 0, 0.85); // 標(biāo)題色
@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')
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容