react-Router嵌套的rouths与webpack刷新错误

时间:2017-01-05 09:59:46

标签: reactjs webpack react-router

当我使用Link to="/Home/PageA"它工作得很好,但是当我刷新页面或在浏览器中输入“/ Home / PageA”时,捆绑包会出错。

WebPack和React-Router之间有一些冲突吗?如何解决?

提前进行。

这是我的图片说明

actPic errorPic

这是我的代码:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, browserHistory, Link } from 'react-router'

    const App = (props) => {
        console.log("App", props);
        const key = 'root';
        return (
            <div>
                {React.cloneElement(props.children || <div />, { key })}
            </div>
        );
    };


    const LoginPage = (props) => {
        console.log("Login", props);

        return (
            <div className="Image">
                <h1>this is LoginPage</h1>
            </div>
        );
    };

    const HomePage = (props) => {
        console.log("Home", props);
        return (
            <div className="Image">
                <li><Link to="/Home/PageA">Tab 1</Link></li>
                <h1>this is  Home page</h1>
                {React.cloneElement(props.children || <div />, { key: props.pathname })}
            </div>
        );
    };

    const PageA = (props) => {
        console.log("pageA", props);
        return (
            <div>
                <input type='button' value="back" onClick={props.router.goBack}></input>
                this is pageA;
            </div>
        );
    };



    ReactDOM.render((
        <Router history={browserHistory}>
            <Route path="/" component={App}>
                <Route path="Login" component={LoginPage}>
                </Route>[enter image description here][1]
                <Route path="Home" component={HomePage}>
                    <Route path="PageA" component={PageA}>
                    </Route>
                </Route>
            </Route>
        </Router>

    ), document.getElementById('container'));

这是我的webPack.config

module.exports = {
    devtool: 'source-map', 
    entry: __dirname + "/app/app.js", 
    output: {
        path: __dirname + "/app", 
        filename: "bundle.js" 
    },
    module: { 
        loaders: [
            {
                test: /\.json$/,
                loader: "json"
            }, {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel', 
                query: {
                    presets: ['es2015', 'react']
                }
            }, {
                test: /\.css$/,
                loader: 'style!css' 
            }, {
                test: /\.(png|jpg)$/,
                loader: 'url?limit=25000'
            }
        ]
    },
    devServer: {
        port: "9023",
        contentBase: "./app", 
        colors: true, 
        historyApiFallback: true, 
        inline: true 
    }
}

1 个答案:

答案 0 :(得分:1)

关于在服务器端进行渲染。您需要具有路由器对象的服务器端映射路径。查看WebpackDevServer

默认使用browserHistory,您可以在此处详细了解如何正确配置:

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory

或者使用哈希历史记录,它更容易完成:

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#hashhistory