安装homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装脚手架:npm install create-react-app
安装yarn:brew install yarn
创建项目:create-react-app react_item_name
安装路由:yarn add react-router-dom
安装axios:yarn add axios
安装antd:yarn add antd
启动项目:npm start

按需引入antd

1、yarn add react-app-rewired customize-cra

/* package.json */
"scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
}

2、根目录下创建config-overrides.js

module.exports = function override(config, env) {
  // do stuff with the webpack config...
  return config;
};

3、yarn add babel-plugin-import

修改config-overrides.js文件:
+ const { override, fixBabelImports } = require('customize-cra'); - module.exports = function override(config, env) { - // do stuff with the webpack config... - return config; - }; + module.exports = override( + fixBabelImports('import', { + libraryName: 'antd', + libraryDirectory: 'es', + style: 'css', + }), + );

这样就可以按需引入antd组件

如:
 // src/App.js
  import React, { Component } from 'react';
+ import { Button } from 'antd';
  import './App.css';

  class App extends Component {
    render() {
      return (
        <div className="App">
          <Button type="primary">Button</Button>
        </div>
      );
    }
  }

  export default App;

但有的时候需要使用less,如修改主题

解决办法如下:

1、yarn add less less-loader

修改config-overrides.js文件:

- const { override, fixBabelImports } = require('customize-cra'); + const { override, fixBabelImports, addLessLoader } = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', - style: 'css', + style: true, }), + addLessLoader({ + javascriptEnabled: true, + modifyVars: { '@primary-color': '#1DA57A' }, + }), );

测试:可以把app.css改成app.less;

  app.js中的引入app.css改成app.less

posted on 2019-05-05 15:29  沉默的末1993  阅读(362)  评论(0编辑  收藏  举报