reactjs - React Router 不会为每条路径重定向
问题描述
我有一个嵌套的反应路由器组件。每当路由匹配 /home/:id 时,就会调用 home 组件,该组件具有更多路由,例如 /home/store。
现在,每当我输入/home/ANY时,它都会成功重定向到/home/store。但它不适用于/home/store/ANY并且我得到一个空页面。
我希望/home/store/ANY旁边的任何内容都应该重定向到/home/store
家庭组件
<Switch>
<Route path='/home/store' exact>
<Store />
</Route>
<Route
path='*'
render={({ location }) => redirectWrapper('/home/store', location)}
/>
</Switch>
重定向包装函数
export function redirectWrapper(path, location) {
return (
<Redirect to={{ pathname: path, state: { from: location } }}></Redirect>
);
}
解决方案
Ciao,你为什么不尝试在 Home 组件中添加一个 Route,例如:
<Switch>
<Route path='/home/store' exact>
<Store />
</Route>
<Route
path='*'
render={({ location }) => redirectWrapper('/home/store', location)}
/>
<Route
path='/home/store/(.*)'
render={({ location }) => redirectWrapper('/home/store', location)}
/>
</Switch>
推荐阅读
- angularjs - 如何在 AngularJS 页面中捕获 nodejs api 数据?
- drupal - 使用来自参考字段的动态参数过滤实体参考视图
- android - 将屏幕截图保存到内部存储并同步到图库?
- angular - Angular 工作区交互
- sql - SQL 列出具有前任的客户经理
- java - Barteksc AndroidPdfViewer 渲染不佳
- android - Glide - 如何在可运行中获取位图
- javascript - 页面速度改进 - DOMContentLoaded vs First Contentful Paint
- c# - 获取“响应状态代码不表示成功:500(内部服务器错误)”错误
- java - 将对象投射到任何接口中?