在为url提供路径时,react-router忽略嵌套路由

疯狂的黑猩猩

我正在与一起玩,reactredux尝试与建立一个基本应用react-routes目前,我已经设法将以下内容组合在一起,这些内容适用于WrapperHome页面,但是如果我转到localhost:8080 / apps,似乎无法加载“应用程序”页面。我每次都得到404。

有什么想法我可能在这里出错了吗?

控制台中没有错误或警告,我尝试过在线浏览几个示例,但是这些示例似乎与我的并没有特别的不同,

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';

import Routes from './routes/routes';
import { configureStore } from './store/configureStore';

const rootElem = document.getElementById('mount');

const store = configureStore(browserHistory);
const history = syncHistoryWithStore(browserHistory, store);

const provider = <Provider store={store}><Routes history={history} /></Provider>;

ReactDOM.render(provider, rootElem);

路线/routes.js

import React from 'react';
import { Router, Route, IndexRoute } from 'react-router'

import { Wrapper, Home, Apps } from './../views';

class Routes extends React.Component {

    render () {

        const { history } = this.props;

        return(
            <Router history={history}>
                <Route path='/' component={Wrapper}>
                    <IndexRoute component={Home}/>
                    <Route path='apps' component={Apps}/>
                </Route>
            </Router>
        );
    }
}

Routes.propTypes = {
    history: React.PropTypes.object.isRequired
};

export default Routes;

存储/配置Store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux'
import createLogger from "redux-logger";
import thunk from 'redux-thunk';

import rootReducer from './../reducers'

export function configureStore(history, initialState = {}) {

    const logger = createLogger();
    const middleware = routerMiddleware(history);

    const store = createStore(
        rootReducer,
        initialState,
        compose(
            applyMiddleware(thunk, middleware, logger),
            window.devToolsExtension ? window.devToolsExtension() : f => f
        )
    );

    return store;
}

reducers / index.js

import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';

export default combineReducers({
    //...reducers,
    routing: routerReducer
});
因鲁邦

您是browserHistory从react-router使用的,因此您的服务器需要能够处理深层链接并仍提供正确的文件。由于您正在连接到端口8080,因此我假设您可能正在使用webpack-dev-server。

您可以切换到hashHistory所有内容然后一切正常。react-router文档解释了如何设置服务器以使其与BrowserHistory一起使用:https : //github.com/reactjs/react-router/blob/master/docs/guides/Histories.md

对于的WebPack-dev的服务器,你只需要通过在配置对象多了一种选择historyApiFallback: true这将使dev-server使用connect-history-api-fallback返回任何深层链接请求的index.html

...
devServer: {
  historyApiFallback: true
}
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章