• react+antd环境配置


    安装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

  • 相关阅读:
    是否可以在tomcat servlet中禁用jsessionid?
    一个屌丝程序猿的人生(一百二十一)
    postman 使用
    【Ubuntu】命令行安装deb安装包
    opencv的cascade分类器源码分析
    Face Detection – OpenCV, Dlib and Deep Learning ( C++ / Python )
    小样本目标检测研究现状
    图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
    搞清楚nand flash和 nor flash 以及 spi flash 和cfi flash 的区别
    xhsell远程vmware ubuntu16.04
  • 原文地址:https://www.cnblogs.com/shui1993/p/10813303.html
Copyright © 2020-2023  润新知