zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Visual Studio 2010CTP的安装
    Crystal Reports中的数据访问
    Web.config配置文件详解1
    asp.net 面向对象方式的传值
    DataSet导出到Excel
    asp.net 身份验证
    Ini文件操作类
    asp.net环境下配置kindEditor文本编辑器
    验证视图状态 MAC 失败解决方法
    Web.config配置文件详解2
  • 原文地址:https://www.cnblogs.com/shui1993/p/10813303.html
Copyright © 2011-2022 走看看