前言:如下內容基於React全家桶+AntD實戰課程的學習實踐過程記錄。最終成果github地址:https://github.com/66Web/react-antd-manager,歡迎star。javascript
1、項目概述css
React全家桶前端
AnD UI組件java
公共機制封裝react
項目總體架構webpack
收穫ios
2、React介紹git
React基礎github
React腳手架、Yarn介紹web
npm install -g create-react-app
create-react-app my-app
cd my-app
npm start
npm install -g yarn
yarn init
yarn add
yarn remove
yarn/yarn install
構建項目的配置
npm install -g yarn
npm install -g create-react-app
create-react-app bike-manager
cd bike-manager
yarn add react-router
yarn remove react-router
實際開發中,不使用react-router,只使用react-router-dom
3、基礎插件安裝,Less文件加載配置
yarn add react-router-dom axios
yarn add antd
yarn eject
注:若是yarn eject 報錯的解決的方法
git add .
git commit -m '註釋'
yarn eject
yarn add less less-loader
yarn start
4、AntD
普通使用:引入UI組件和css文件
@import 'antd/dist/antd.css'
import {Button} from 'antd'
按需加載css文件內容【只會打包所需組件,減小請求量】
yarn add babel-plugin-import
實現按需加載所需的css,而不是每次都引入全部的css
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve(
'babel-preset-react-app/webpack-overrides'
),
plugins: [
//要插入的內容
['import', {
libraryName: 'antd', //libraryName表示導出的庫名,
style: 'css' //style表示將css引入項目行內樣式
}],
這個在package.json/babelrl/webpack.config.js中配置均可以,pugins是個二維數組!!
至關於直接把antd.less文件引入到項目js中,不用再引入import 'antd/dist/antd.css'
定製主題:修改less變量
"style": true //true標識直接加載antd的less文件
if (preProcessor) {
let loader = {
loader: require.resolve(preProcessor),
options: {
sourceMap: shouldUseSourceMap,
},
}
if (preProcessor === "less-loader") {
loader.options.modifyVars = {
'primary-color': '#f9c700'
}
loader.options.javascriptEnabled = true
}
loaders.push(loader);
}
注:項目來自慕課網