gpt4 book ai didi

javascript - react-router 在提供带路径的 url 时忽略嵌套路由

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:11 28 4
gpt4 key购买 nike

我正在尝试使用 reactredux 尝试将基本应用程序与 react-routes 放在一起。目前我已经设法将以下内容放在一起,适用于 WrapperHome 页面,但是如果我去,“应用程序”页面似乎不会加载本地主机:8080/应用程序。我每次都会收到 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/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。这将使开发服务器为任何深度链接的请求返回 index.html,使用 connect-history-api-fallback .

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

关于javascript - react-router 在提供带路径的 url 时忽略嵌套路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com