微之 發表於 2019-10-11 11:09:00

使用create-react-app+react-router-dom+axios+antd+react-redux构建react项目

<h2>1、安装、构建</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># 全局安装
npm install </span>-g create-react-<span style="color: rgba(0, 0, 0, 1)">app
# 构建一个my</span>-<span style="color: rgba(0, 0, 0, 1)">app的项目
npx create</span>-react-app my-<span style="color: rgba(0, 0, 0, 1)">app
cd my</span>-<span style="color: rgba(0, 0, 0, 1)">app

# 启动编译当前的React项目,并自动打开 http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:3000/</span>
npm start</pre>
</div>
<h2>2、项目目录</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">//默认<br><br>├── package.json
├── public                  # 这个是webpack的配置的静态目录
│   ├── favicon.ico
│   ├── index.html          # 默认是单页面应用,这个是最终的html的基础模板
│   └── manifest.json
├── src
│   ├── App.css             # App根组件的css
│   ├── App.js            # App组件代码
│   ├── App.test.js
│   ├── index.css         # 启动文件样式
│   ├── index.js            # 启动的文件(开始执行的入口)!!!!
│   ├── logo.svg
│   └── serviceWorker.js
└── yarn.lock</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改</span>
<span style="color: rgba(0, 0, 0, 1)">├── package.json
├── public                  # 这个是webpack的配置的静态目录
│   ├── favicon.ico
│   ├── index.html          # 默认是单页面应用,这个是最终的html的基础模板
│   └── manifest.json
├── src
│   ├── assets            # 图片等静态资源
│   ├── redux             # 状态
│   │      ├── action.js               # action
│   │      ├──reducerjs            # reducer
│   │      └── index.js               # 主文件
│   ├── router             # 路由
│   │      ├── config.js               # 配置
│   │      ├── FrontendAuth.js      # 路由守卫
│   │      └── index.js               # 主文件
│   ├── serve            # 请求
│   │      ├── index.js            # axio
│   ├── views            # 页面
│   ├── App.css             # App根组件的css
│   ├── App.js            # App组件代码
│   ├── App.test.js
│   ├── index.css         # 启动文件样式
│   ├── index.js            # 启动的文件(开始执行的入口)!!!!
│   ├── logo.svg
│   └── serviceWorker.js
└── yarn.lock          </span></pre>
</div>
<p>&nbsp;</p>
<h2>3、antd</h2>
<div class="cnblogs_code">
<pre>yarn add antd</pre>
</div>
<p>修改&nbsp;<code>src/App.css</code>,在文件顶部引入&nbsp;<code>antd/dist/antd.css</code>。</p>
<div class="cnblogs_code">
<pre>@import '~antd/dist/antd.css'<span style="color: rgba(0, 0, 0, 1)">;

.App {
text</span>-<span style="color: rgba(0, 0, 0, 1)">align: center;
}

...</span></pre>
</div>
<p><code>antd</code>&nbsp;目前的默认文案是英文,如果需要使用其他语言,可以参考下面的方案。</p>
<p>antd 提供了一个 React 组件&nbsp;ConfigProvider&nbsp;用于全局配置国际化文案。</p>
<div class="cnblogs_code">
<pre>import zhCN from 'antd/es/locale/zh_CN'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span>&lt;ConfigProvider locale={zhCN}&gt;
    &lt;App /&gt;
&lt;/ConfigProvider&gt;
);</pre>
</div>
<h2>4、axios</h2>
<div class="cnblogs_code">
<pre>yarn add axios</pre>
</div>
<p>/src/serve/index.js,统一配置并绑定到react上</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import axios from </span>'axios'<span style="color: rgba(0, 0, 0, 1)">;
axios.defaults.headers[</span>'Content-Type'] = 'application/json'<span style="color: rgba(0, 0, 0, 1)">;


let config </span>=<span style="color: rgba(0, 0, 0, 1)"> {
baseURL: </span>''<span style="color: rgba(0, 0, 0, 1)">,
timeout: </span>60 * 1000, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Timeout</span>
<span style="color: rgba(0, 0, 0, 1)">};
const _axios </span>=<span style="color: rgba(0, 0, 0, 1)"> axios.create(config);
_axios.interceptors.request.use(
(config) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do something before request is sent</span>
    config.headers['Authorization'] = ''<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> config;
},
(error) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do something with request error</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Promise.reject(error);
}
);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Add a response interceptor</span>
<span style="color: rgba(0, 0, 0, 1)">_axios.interceptors.response.use(
(response) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do something with response data</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> response
},
(error) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do something with response error</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Promise.reject();
}
);
React.Component.prototype.$axios </span>= _axios;</pre>
</div>
<h2>5、router</h2>
<div class="cnblogs_code">
<pre>yarn add react-router-dom</pre>
</div>
<p>/src/router/config.js</p>
<div class="cnblogs_code">
<pre>import login from '../views/login'<span style="color: rgba(0, 0, 0, 1)">;
import record from </span>'../views/record'<span style="color: rgba(0, 0, 0, 1)">;
import home from </span>'../views/home'<span style="color: rgba(0, 0, 0, 1)">;


export const routerConfig </span>=<span style="color: rgba(0, 0, 0, 1)"> [
{
    path:</span>'/'<span style="color: rgba(0, 0, 0, 1)">,
    component:home,
    auth:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
},{
    path:</span>'/record'<span style="color: rgba(0, 0, 0, 1)">,
    component:record,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">auth:true,</span>
<span style="color: rgba(0, 0, 0, 1)">},{
    path:</span>'/login'<span style="color: rgba(0, 0, 0, 1)">,
    component:login,
}
];</span></pre>
</div>
<p>/src/router/FrontendAuth.js</p>
<div class="cnblogs_code">
<pre>import React, {Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import {Route, Redirect} from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">;

export class FrontendAuth extends Component {
render() {
    const {location, config} </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;
    const {pathname} </span>=<span style="color: rgba(0, 0, 0, 1)"> location;
    const isLogin </span>= localStorage.getItem('__config_center_token'<span style="color: rgba(0, 0, 0, 1)">)

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果该路由不用进行权限校验,登录状态下登陆页除外</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为登陆后,无法跳转到登陆页</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这部分代码,是为了在非登陆状态下,访问不需要权限校验的路由</span>
    const targetRouterConfig = config.find((v) =&gt; v.path ===<span style="color: rgba(0, 0, 0, 1)"> pathname);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (targetRouterConfig &amp;&amp; !targetRouterConfig.auth &amp;&amp; !<span style="color: rgba(0, 0, 0, 1)">isLogin) {
      const {component} </span>=<span style="color: rgba(0, 0, 0, 1)"> targetRouterConfig;
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Route exact path={pathname} component={component}/&gt;
<span style="color: rgba(0, 0, 0, 1)">    }

    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isLogin) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果是登陆状态,想要跳转到登陆,重定向到主页</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (pathname === '/login'<span style="color: rgba(0, 0, 0, 1)">) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Redirect to='/'/&gt;
      } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果路由合法,就跳转到相应的路由</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (targetRouterConfig) {
          </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Route path={pathname} component={targetRouterConfig.component}/&gt;
      } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果路由不合法,重定向到 404 页面</span>
          <span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Redirect to='/404'/&gt;
<span style="color: rgba(0, 0, 0, 1)">      }
      }
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 非登陆状态下,当路由合法时且需要权限校验时,跳转到登陆页面,要求登陆</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (targetRouterConfig &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> targetRouterConfig.auth) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Redirect to='/login'/&gt;
      } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 非登陆状态下,路由不合法时,重定向至 404</span>
      <span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Redirect to='/404'/&gt;
<span style="color: rgba(0, 0, 0, 1)">      }
    }
}
}</span></pre>
</div>
<p>/src/router/index.js</p>
<div class="cnblogs_code">
<pre>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import { Switch} from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">;

import { FrontendAuth } from </span>'./FrontendAuth'<span style="color: rgba(0, 0, 0, 1)">;
import { routerConfig } from </span>'./config'<span style="color: rgba(0, 0, 0, 1)">;


class Routes extends Component {
render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Switch&gt;
      &lt;FrontendAuth config={routerConfig} /&gt;
      &lt;/Switch&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Routes</pre>
</div>
<p>app.js</p>
<div class="cnblogs_code">
<pre>import {BrowserRouter as Router} from 'react-router-dom';<br>import Routes from './router/index'</pre>
<pre><em id="__mceDel"><span style="color: rgba(0, 0, 0, 1)">
class App extends Component{
render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Router&gt;
      &lt;Routes&gt;&lt;/Routes&gt;
      &lt;/Router&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App;</em></pre>
</div>
<h2>6、scss</h2>
<p>&nbsp;安装完后,只要把sass文件改成.scss就行了</p>
<div class="cnblogs_code">
<pre>npm install node-sass sass-loader --save</pre>
</div>
<h2>7、修改端口号</h2>
<p>项目默认端口号是3000,如下可以更改:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">在package.json中修改
</span>"start":"react-scripts start"<span style="color: rgba(0, 0, 0, 1)">,

</span>"start":"set PORT=9000 &amp;&amp; react-scripts start",</pre>
</div>
<h2>8、react-redux</h2>
<p>redux不是项目必需的,如果你不确定是否需要,那就是不需要,在react无法实现时,再考虑。</p>
<div class="cnblogs_code">
<pre>npm install react-redux redux --S</pre>
</div>
<p>&nbsp;/src/redux/action.js</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* action 类型
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

export const COUNT </span>= 'COUNT'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* 初始值
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

export const config </span>=<span style="color: rgba(0, 0, 0, 1)"> {
    count:</span>1000<span style="color: rgba(0, 0, 0, 1)">
};


</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* action 创建函数
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

export </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> setCount(value) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> { type: COUNT, value }
}</span></pre>
</div>
<p>/src/redux/reducer.js</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这是action</span>
<span style="color: rgba(0, 0, 0, 1)">import {
COUNT
} from </span>'./actions'

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这是reducer</span>
const reducer = (state , action) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (action.type) {
    </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> COUNT:
      let count </span>=<span style="color: rgba(0, 0, 0, 1)">action.value;
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {...state,...{count}};
    </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> state;
}
};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> reducer</pre>
</div>
<p>/src/redux/index.js</p>
<div class="cnblogs_code">
<pre>import { createStore } from 'redux'<span style="color: rgba(0, 0, 0, 1)">;
import {config} from </span>'./actions'<span style="color: rgba(0, 0, 0, 1)">;
import reducer from </span>'./reducer'<span style="color: rgba(0, 0, 0, 1)">;
let store </span>=<span style="color: rgba(0, 0, 0, 1)"> createStore(reducer,config);

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> store</pre>
</div>
<p>/src/index.js</p>
<div class="cnblogs_code">
<pre>import { Provider } from 'react-redux'<span style="color: rgba(0, 0, 0, 1)">
import store from </span>'./redux/index'<span style="color: rgba(0, 0, 0, 1)">

ReactDOM.render(
</span>&lt;ConfigProvider locale={zhCN}&gt;
    &lt;Provider store={store}&gt;
      &lt;App /&gt;
    &lt;/Provider&gt;
&lt;/ConfigProvider&gt;, document.getElementById('root'));</pre>
</div>
<p>//页面调用示例</p>
<div class="cnblogs_code">
<pre>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import { connect } from </span>'react-redux'<span style="color: rgba(0, 0, 0, 1)">;
import {
setCount,
} from </span>'../redux/actions'<span style="color: rgba(0, 0, 0, 1)">

class ReduxTest extends Component {

componentDidMount() {
    console.log(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props)
}
render() {
    const { PayIncrease,tiger } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div className="App"&gt;
      &lt;h2&gt;当月工资为{tiger}&lt;/h2&gt;
      &lt;button onClick={PayIncrease}&gt;升职加薪&lt;/button&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要渲染什么数据</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> mapStateToProps(state) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
    tiger: state.count
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要触发什么行为</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> mapDispatchToProps(dispatch) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
    PayIncrease: () </span>=&gt; dispatch(setCount(99999<span style="color: rgba(0, 0, 0, 1)">)),
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> ReduxTest = connect(mapStateToProps, mapDispatchToProps)(ReduxTest)</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/gxp69/p/11641560.html
頁: [1]
查看完整版本: 使用create-react-app+react-router-dom+axios+antd+react-redux构建react项目