反应路由器在提供 url 路径时忽略嵌套路由
react-router ignoring nested routes when providing url with path
我正在研究 react
和 redux
,试图用 react-routes
组合一个基本应用程序。目前我已经设法将以下内容放在一起,适用于 Wrapper
和 Home
页面,但是如果我转到 [=46],'apps' 页面似乎无法加载=]/应用程序。我每次都收到 404。
有什么我在这里可能出错的想法吗?
控制台中没有错误或警告,我尝试在线查看几个示例,但是 none 中的示例似乎与我所拥有的特别不同,
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/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/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;
}
reducers/index.js
import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';
export default combineReducers({
//...reducers,
routing: routerReducer
});
您正在使用来自 react-router 的 browserHistory
,因此您的服务器需要能够处理深层链接并仍然提供正确的文件。由于您正在连接到端口 8080,我假设您可能正在使用 webpack-dev-server。
您可以切换到 hashHistory
,一切都会正常进行。 react-router 文档解释了如何设置服务器以使用 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
. This will make the dev-server return the index.html for any deeply-linked requests, using connect-history-api-fallback.
...
devServer: {
historyApiFallback: true
}
...
我正在研究 react
和 redux
,试图用 react-routes
组合一个基本应用程序。目前我已经设法将以下内容放在一起,适用于 Wrapper
和 Home
页面,但是如果我转到 [=46],'apps' 页面似乎无法加载=]/应用程序。我每次都收到 404。
有什么我在这里可能出错的想法吗?
控制台中没有错误或警告,我尝试在线查看几个示例,但是 none 中的示例似乎与我所拥有的特别不同,
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/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/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;
}
reducers/index.js
import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';
export default combineReducers({
//...reducers,
routing: routerReducer
});
您正在使用来自 react-router 的 browserHistory
,因此您的服务器需要能够处理深层链接并仍然提供正确的文件。由于您正在连接到端口 8080,我假设您可能正在使用 webpack-dev-server。
您可以切换到 hashHistory
,一切都会正常进行。 react-router 文档解释了如何设置服务器以使用 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
. This will make the dev-server return the index.html for any deeply-linked requests, using connect-history-api-fallback.
...
devServer: {
historyApiFallback: true
}
...