小白搭建webpack+react+redux+es6(三)

2018-11-12  本文已影响115人  一支桨

代码仓库地址:webpack+react+redux+es6

上篇文章有个坑,有个axios的报错,当时是已经配置好的环境,写文档的时候马虎了没删除,安装这个依赖就好了,本文也会介绍如何使用

npm install axios

此时你就能使用RESTful API去get、put等方式去请求数据了

 //浏览器配置
    devServer: {
        port: 8888,
        contentBase: './public',
        inline: true,
        openPage: '',
        //后台数据请求接口配置
        proxy: {
            "/api": {
                target: "http://localhost:3000",
                secure: false,
                pathRewrite: {
                    "/api/": ""
                }
            }
        },
        overlay: {
            warnings: true,
            errors: true
        }
    }

安装react-router相关依赖

npm i react-router react-router-dom -S
//route.jsx
import React from 'react'
import { HashRouter as Router, Switch, Route, Link, Redirect } from 'react-router-dom'

import { Count, Like } from './index.jsx'
const routes = (
    <Router>
        <div>
            <Link to='/like' replace>走你</Link>
            <Switch>
                <Route exact path='/' component={Count}></Route>
                <Route path='/like' component={Like}></Route>
                <Redirect to="/" />
            </Switch>
        </div>
    </Router>
)
export default routes
//...
import routes from './components/route.jsx';
//...
render(
    <Provider store={store}>
        {routes}
    </Provider>
    ,document.getElementById("root")
)
//...

结束语

综上三文,除样式外,所有的配置都已涉及到,各文件的函数功能未详细描述,请自查官档,这里不作描述,谢谢大家

上一篇下一篇

猜你喜欢

热点阅读