react 后台(一) react + redux + react-route + webpack+ axios + antd + less

create-react-app 项目名称(项目失败,ant 的样式出不来)

项目技术栈

react + redux + react-route + webpack+ axios + less + antdcss

 

使用create-react-app 建立的项目默认不支持less,如下增长less配置的步骤html

暴露配置文件
create-react-app生成的项目文,看不到webpack相关的配置文件,须要先暴露出来,使用以下命令便可:react

npm run eject

安装less-loader 和 lesswebpack

npm install less-loader less --save-dev

yarn less-loader less//这个执行不行

此时没有webpack.config.js文件,而后执行npm run eject暴露webpack.config.js文件,在config文件夹里(此操做不可逆)ios

运行后若是没法启动,执行 npm installweb

老版修改方式  
修改webpack配置
修改 webpack.config.dev.js 和 webpack.config-prod.js 配置文件npm

改动1:

/\.css$/ 改成 /\.(css|less)$/,, 修改后以下:

exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.(css|less)$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
改动2:

test: /\.css$/ 改成 /\.(css|less)$/

 新版修改方式json

config文件此目录 webpack.config.jsredux

1axios

// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;

改成

// style files regexes
const cssRegex = /\.(css|less)$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;

2

  // common function to get style loaders
  const getStyleLoaders = (cssOptions, preProcessor) => {
    const loaders = [
      isEnvDevelopment && require.resolve('style-loader'),
      isEnvProduction && {
        loader: MiniCssExtractPlugin.loader,
        options: shouldUseRelativeAssetPaths ? { publicPath: '../../' } : {},
      },
      {
        loader: require.resolve('css-loader'),
        options: cssOptions,
      },
      {

增长代码

  const getStyleLoaders = (cssOptions, preProcessor) => {
    const loaders = [
      isEnvDevelopment && require.resolve('style-loader'),
      isEnvProduction && {
        loader: MiniCssExtractPlugin.loader,
        options: shouldUseRelativeAssetPaths ? { publicPath: '../../' } : {},
      },
      {
        loader: require.resolve('css-loader'),
        options: cssOptions,
      },
      ,
 { loader: require.resolve('less-loader'),
        options: cssOptions,
      },

 先验证less是否能够

import './app.less';

 

否则刷新不行,就重启

若是antd引入的组件仍是不展现样式

在引入的页面添加

import 'antd/dist/antd.css';

 刷新就能看到样式了

 若是想实现按需加载antd,须要引入

yarn add babel-plugin-import

修改 package.json,添加下面代码就能够去掉 import 'antd/dist/antd.css';

  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
    ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }] ]
  }

 在或者接着添加 customize-cra

yarn add customize-cra

 

建立文件 config-overrides.js

const { override, fixBabelImports } = require('customize-cra');
module.exports = override(
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: 'css',
  }),
);

而后重启运行

 

yarn add antd

yarn add axios

yarn add react-redux

yarn add react-router

yarn add react-router-dom

yarn add redux-immutable

yarn add react-router-redux

yarn add redux

yarn add redux-actions

yarn add redux-mock-store

yarn add redux-thunk

yarn add immutable

yarn add echarts

yarn add md5

yarn add rc-queue-anim

yarn add rc-tween-one

yarn add prop-types

若是会用的日期要再引入一个模块,用来格式化日期的

yarn add moment

若是有跨域请求

yarn add  fetch-jsonp

 

import { Select } from 'antd';
import jsonp from 'fetch-jsonp';
import querystring from 'querystring';

const { Option } = Select;

let timeout;
let currentValue;

function fetch(value, callback) {
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  currentValue = value;

  function fake() {
    const str = querystring.encode({
      code: 'utf-8',
      q: value,
    });
//这里 jsonp(`https:
//suggest.taobao.com/sug?${str}`) .then(response => response.json()) .then(d => { if (currentValue === value) { const { result } = d; const data = []; result.forEach(r => { data.push({ value: r[0], text: r[0], }); }); callback(data); } }); } timeout = setTimeout(fake, 300); } class SearchInput extends React.Component { state = { data: [], value: undefined, }; handleSearch = value => { fetch(value, data => this.setState({ data })); }; handleChange = value => { this.setState({ value }); }; render() { const options = this.state.data.map(d => <Option key={d.value}>{d.text}</Option>); return ( <Select showSearch value={this.state.value} placeholder={this.props.placeholder} style={this.props.style} defaultActiveFirstOption={false} showArrow={false} filterOption={false} onSearch={this.handleSearch} onChange={this.handleChange} notFoundContent={null} > {options} </Select> ); } } ReactDOM.render(<SearchInput placeholder="input search text" style={{ width: 200 }} />, mountNode);
相关文章
相关标签/搜索