我开始在我的应用程序中使用react-router,发现当URL末尾带有斜杠(/url/
)时,它无法正常工作。我搜索了更多相关内容,阅读了所有文档和react-router问题,并尝试使用<Redirect from='/*/' to="/*" />
,但这并不是一个好的解决方案,因为它也无法正常工作。因此,我继续阅读,发现有一个建议是在URL末尾插入/?
,但仍然没有起作用。
路由代码:
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在结尾处没有任何斜杠。你有什么建议可以帮忙实现吗?为什么重定向
在这种情况下没有起作用?