# 全局安裝 npm install -g create-react-app # 構建一個my-app的項目 npx create-react-app my-app cd my-app # 啓動編譯當前的React項目,並自動打開 http://localhost:3000/ npm start
//默認
├── 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
//修改 ├── 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
yarn add antd
修改 src/App.css
,在文件頂部引入 antd/dist/antd.css
。css
@import '~antd/dist/antd.css'; .App { text-align: center; } ...
antd
目前的默認文案是英文,若是須要使用其餘語言,能夠參考下面的方案。html
antd 提供了一個 React 組件 ConfigProvider 用於全局配置國際化文案。node
import zhCN from 'antd/es/locale/zh_CN'; return ( <ConfigProvider locale={zhCN}> <App /> </ConfigProvider> );
yarn add axios
/src/serve/index.js,統一配置並綁定到react上react
import React from 'react'; import axios from 'axios'; axios.defaults.headers['Content-Type'] = 'application/json'; let config = { baseURL: '', timeout: 60 * 1000, // Timeout }; const _axios = axios.create(config); _axios.interceptors.request.use( (config) => { // Do something before request is sent config.headers['Authorization'] = ''; return config; }, (error) => { // Do something with request error return Promise.reject(error); } ); // Add a response interceptor _axios.interceptors.response.use( (response) => { // Do something with response data return response }, (error) => { // Do something with response error return Promise.reject(); } ); React.Component.prototype.$axios = _axios;
yarn add react-router-dom
/src/router/config.jswebpack
import login from '../views/login'; import record from '../views/record'; import home from '../views/home'; export const routerConfig = [ { path:'/', component:home, auth:true, },{ path:'/record', component:record, //auth:true, },{ path:'/login', component:login, } ];
/src/router/FrontendAuth.jsios
import React, {Component} from 'react'; import {Route, Redirect} from 'react-router-dom'; export class FrontendAuth extends Component { render() { const {location, config} = this.props; const {pathname} = location; const isLogin = localStorage.getItem('__config_center_token') // 若是該路由不用進行權限校驗,登陸狀態下登錄頁除外 // 由於登錄後,沒法跳轉到登錄頁 // 這部分代碼,是爲了在非登錄狀態下,訪問不須要權限校驗的路由 const targetRouterConfig = config.find((v) => v.path === pathname); if (targetRouterConfig && !targetRouterConfig.auth && !isLogin) { const {component} = targetRouterConfig; return <Route exact path={pathname} component={component}/> } if (isLogin) { // 若是是登錄狀態,想要跳轉到登錄,重定向到主頁 if (pathname === '/login') { return <Redirect to='/'/> } else { // 若是路由合法,就跳轉到相應的路由 if (targetRouterConfig) { return <Route path={pathname} component={targetRouterConfig.component}/> } else { // 若是路由不合法,重定向到 404 頁面 return <Redirect to='/404'/> } } } else { // 非登錄狀態下,當路由合法時且須要權限校驗時,跳轉到登錄頁面,要求登錄 if (targetRouterConfig && targetRouterConfig.auth) { return <Redirect to='/login'/> } else { // 非登錄狀態下,路由不合法時,重定向至 404 return <Redirect to='/404'/> } } } }
/src/router/index.jsweb
import React, { Component } from 'react'; import { Switch} from 'react-router-dom'; import { FrontendAuth } from './FrontendAuth'; import { routerConfig } from './config'; class Routes extends Component { render() { return ( <Switch> <FrontendAuth config={routerConfig} /> </Switch> ) } } export default Routes
app.jsnpm
import {BrowserRouter as Router} from 'react-router-dom';
import Routes from './router/index'
class App extends Component { render() { return ( <Router> <Routes></Routes> </Router> ); } } export default App;
安裝完後,只要把sass文件改爲.scss就好了json
npm install node-sass sass-loader --save
項目默認端口號是3000,以下能夠更改:redux
在package.json中修改 "start":"react-scripts start", 爲 "start":"set PORT=9000 && react-scripts start",
redux不是項目必需的,若是你不肯定是否須要,那就是不須要,在react沒法實現時,再考慮。
npm install react-redux redux --S
/src/redux/action.js
/* * action 類型 */ export const COUNT = 'COUNT'; /* * 初始值 */ export const config = { count:1000 }; /* * action 建立函數 */ export function setCount(value) { return { type: COUNT, value } }
/src/redux/reducer.js
//這是action import { COUNT } from './actions' //這是reducer const reducer = (state , action) => { switch (action.type) { case COUNT: let count = action.value; return {...state,...{count}}; default: return state; } }; export default reducer
/src/redux/index.js
import { createStore } from 'redux'; import {config} from './actions'; import reducer from './reducer'; let store = createStore(reducer,config); export default store
/src/index.js
import { Provider } from 'react-redux' import store from './redux/index' ReactDOM.render( <ConfigProvider locale={zhCN}> <Provider store={store}> <App /> </Provider> </ConfigProvider>, document.getElementById('root'));
//頁面調用示例
import React, { Component } from 'react'; import { connect } from 'react-redux'; import { setCount, } from '../redux/actions' class ReduxTest extends Component { componentDidMount() { console.log(this.props) } render() { const { PayIncrease,tiger } = this.props; return ( <div className="App"> <h2>當月工資爲{tiger}</h2> <button onClick={PayIncrease}>升職加薪</button> </div> ); } } //須要渲染什麼數據 function mapStateToProps(state) { return { tiger: state.count } } //須要觸發什麼行爲 function mapDispatchToProps(dispatch) { return { PayIncrease: () => dispatch(setCount(99999)), } } export default ReduxTest = connect(mapStateToProps, mapDispatchToProps)(ReduxTest)