4

我正在玩弄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);

路线/路线.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;

存储/configureStore.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;
}

减速器/index.js

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

export default combineReducers({
    //...reducers,
    routing: routerReducer
});
4

1 回答 1

5

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

你可以切换到hashHistory,一切都会好起来的。react-router 文档解释了设置服务器以使用 browserHistory:https ://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md

对于 webpack-dev-server,你只需要在配置对象中传递一个额外的选项historyApiFallback: true。这将使开发服务器使用connect-history-api-fallback返回任何深度链接请求的 index.html 。

...
devServer: {
  historyApiFallback: true
}
...
于 2016-04-09T20:26:33.127 回答