【发布时间】:2017-01-31 01:14:55
【问题描述】:
我开始在我的应用程序中使用 react-router,我注意到当它在 URL (/url/) 的末尾有一个斜杠时它不起作用。我对其进行了更多搜索,阅读了所有文档和 react-router 问题并尝试使用<Redirect from='/*/' to="/*" />,但这不是一个好的解决方案,因为它也不起作用。因此,阅读更多内容后,我发现了在 URL 末尾插入 /? 的建议,但它仍然无效。
routes.js的代码:
export default (
<Route path="/" component={App}>
<IndexRoute component={ProfileFillComponents} />
<Route path="/seguro-residencia" handler={require('./components/Forms/Residencial/ProfileFill/profileFillComponents')} />
<Route path="/seguro-residencia/informacoes-pessoais" component={CotationNumber} />
</Route>
)
index.js的代码:
render((<Router history={browserHistory} routes={routes} />), document.getElementById('root'));
搜索更多,我发现一个人做了一个函数来强制在 URL 上使用斜杠,我决定做相反的事情,强迫没有它。
使用函数更新 routes.js 代码没有尾部斜杠函数:
export default (
<Route onEnter={forceTrailingSlash} onChange={forceTrailingSlashOnChange}>
<Route path="/" component={App}>
<IndexRoute component={ProfileFillComponents} />
<Route path="/seguro-residencia" handler={require('./components/Forms/Residencial/ProfileFill/profileFillComponents')} />
<Route path="/seguro-residencia/informacoes-pessoais" component={CotationNumber} />
</Route>
</Route>
)
function forceNoTrailingSlash(nextState, replace) {
const path = nextState.location.pathname;
if (path.slice(-1) === '/') {
replace({
...nextState.location,
pathname: path.slice(1,path.lastIndexOf('/')-1)
});
}
}
function forceNoTrailingSlashOnChange(prevState, nextState, replace) {
forceNoTrailingSlash(nextState, replace);
}
这又没用!我需要使这些 URL 尽可能友好,并且我希望 URL 末尾没有任何尾随斜杠。有什么建议我该怎么做?为什么Redirect 在这种情况下不起作用?
【问题讨论】:
标签: javascript reactjs react-router