【发布时间】:2019-04-21 20:20:30
【问题描述】:
我正在使用 React 和 React-router v4
这是我的路由组件:
<Switch>
{/* <Route path='/blog' exact component={Blog} /> */}
<Route path='/projects/:id' component={ProjectDetails} />
<Route path='/career/:id' component={CareerDetails} />
<Route path='/' component={withScrollPreservation(LandingPage)} />
<Route component={withScrollPreservation(LandingPage)} />
</Switch>
我的问题是什么:
如果用户从路由中输入了无效的内容,我希望它重定向到主页。考虑在本地运行这个场景:
localhost:4000/ - 主页
localhiost:4000/invalidurl - 应该重定向回localhost:4000/ 并从 url 中删除 invalidurl
有什么想法吗?
【问题讨论】:
-
可以使用Redirect组件
-
@ShubhamKhatri - 当用户在浏览器上手动输入时遇到问题,例如 localhost:9090/{invalidurl}/{invalidUrl} 。我也提出了这个问题,链接是stackoverflow.com/questions/64261462/…。请给出一些想法,如何做到这一点,
标签: reactjs redirect react-router url-redirection react-router-v4