私は自分のアプリケーションでreact-routerを使い始めていますが、URLの末尾にスラッシュがあるときは機能しません。私はそれについてさらに検索し、すべてのドキュメントと反応ルータの問題を読んで、<Redirect from='/*/' to="/*" />
を使ってみましたが、良い解決策ではありませんでした。だから、私はURLの末尾に/?
を挿入する提案を見つけたが、それでも読むことはできませんでした。反応ルータの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をできるだけフレンドリーにする必要があり、最後に末尾にスラッシュがないようにしたいと思います。どのように私はこれを行うことができますどのような提案?そしてなぜRedirect
がこの場合にはうまくいかなかったのですか?
解決方法はありましたか?共有していただけますか? – myDoggyWritesCode
はい、持っています!私はすぐに私のソリューションを掲載します。 :) – Aipi
素晴らしい。 :)ありがとう – myDoggyWritesCode