我正在玩react
和redux
试图将基本应用与react-routes
组合在一起。目前,我已设法将以下内容放在一起,适用于Wrapper
和Home
页面,但是如果我转到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;
商品/ 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
});
答案 0 :(得分:5)
您正在使用来自react-router的browserHistory
,因此您的服务器需要能够处理深层链接并仍然提供正确的文件。由于您正在连接到端口8080,我假设您可能正在使用webpack-dev-server。
您可以切换到hashHistory
,一切正常。 react-router docs解释了如何设置服务器以使用browserHistory:https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md
对于webpack-dev-server,您只需pass an extra option in the config object:historyApiFallback: true
。这将使dev-server使用connect-history-api-fallback为任何深度链接的请求返回index.html。
...
devServer: {
historyApiFallback: true
}
...