1.安装省略
下载ant
yarn add antd
2. 下载按需加载命令
下载依赖模块
yarn add react-app-rewired customize-cra babel-plugin-import
定义加载配置的 js 模块: config-overrides.js
const {override, fixBabelImports} = require('customize-cra');
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd',
libraryDirectory: 'es',
style: 'css',
}),
);
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
Vscode 的 react 代码保存后,代码格式乱了怎么办
JS-CSS-HTML Formatter的插件, 把这个插件禁用后就OK了
import React, {Component} from 'react'
import {Button, message} from 'antd'
eject
options: {
plugins: [
['import',[{ // 导入一个插件
libraryName: 'antd', // 暴露的库名
style: true // 直接将ants样式文件动态编译成行内样式插入,就不需要每次都导入
}]]
],
cacheDirectory: true,
},
和
options: {
plugins: [
['import',[{ // 导入一个插件
libraryName: 'antd', // 暴露的库名
style: true // 直接将ants样式文件动态编译成行内样式插入,就不需要每次都导入
}]]
],
compact: true,
},
import { Button } from 'antd';
这种方式必须暴露配置,这回让我们开发的时候不是很方便,所以推荐下面的这种方式
npm i [email protected] babel-plugin-import customize-cra
这个时候就会有一个比较大的坑,react-app-rewired必须制定版本,否则报错
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
修改后的版本
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
config-overrides.js
const {
override,
fixBabelImports,
addLessLoader,
} = require("customize-cra");
module.exports = override(
fixBabelImports("import", {
libraryName: "antd", libraryDirectory: "es", style: true // change importing css to less
}),
addLessLoader({
javascriptEnabled: true,
modifyVars: { "@primary-color": "#1DA57A" }
})
);
import { Button } from 'antd';
使用
react-app-rewired
来代替react-scripts
的启动方式,并且安装所需要的依赖,在根目录创建config-overrides.js
文件,导入配置即可完成
自定义组件 import 前面必须要有. 如./ 组件名首字母必须大写
下载工具包:
yarn add less less-loader
修改 config-overrides.js
const {override, fixBabelImports, addLessLoader} = require('customize-cra');
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd',
libraryDirectory: 'es',
style: true, //自动加载less文件
}),
addLessLoader({
javascriptEnabled: true,
modifyVars: {'@primary-color': '#1DA57A'},
}),
);
下载路由包: react-router-dom
yarn add react-router-dom
pages中文件使用isx结尾的文件名 component文件使用index.js这样好区分
yarn add axios