如何在 React Router 中将 DefaultRoute 设置为另一个路由 [英] How to set the DefaultRoute to another Route in React Router

查看:32
本文介绍了如何在 React Router 中将 DefaultRoute 设置为另一个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

  <Route name="app" path="/" handler={App}>
    <Route name="dashboards" path="dashboards" handler={Dashboard}>
      <Route name="exploreDashboard" path="exploreDashboard" handler={ExploreDashboard} />
      <Route name="searchDashboard" path="searchDashboard" handler={SearchDashboard} />
      <DefaultRoute handler={DashboardExplain} />
    </Route>
    <DefaultRoute handler={SearchDashboard} />
  </Route>

使用 DefaultRoute 时,SearchDashboard 呈现错误,因为任何 *Dashboard 都需要在 Dashboard 中呈现.

When using the DefaultRoute, SearchDashboard renders incorrectly since any *Dashboard needs to rendered within Dashboard.

我希望应用程序"路由中的 DefaultRoute 指向searchDashboard"路由.这是我可以用 React Router 做的事情,还是我应该为此使用普通的 Javascript(用于页面重定向)?

I would like for my DefaultRoute within the "app" Route to point to the Route "searchDashboard". Is this something that I can do with React Router, or should I use normal Javascript (for a page redirect) for this?

基本上,如果用户转到主页,我想将它们发送到搜索仪表板.所以我想我正在寻找一个相当于 window.location.replace("mygreathostname.com/#/dashboards/searchDashboard");

Basically, if the user goes to the home page I want to send them instead to the search dashboard. So I guess I'm looking for a React Router feature equivalent to window.location.replace("mygreathostname.com/#/dashboards/searchDashboard");

推荐答案

您可以使用 Redirect 而不是 DefaultRoute

You can use Redirect instead of DefaultRoute

<Redirect from="/" to="searchDashboard" />

更新 2019-08-09 以避免刷新问题,请改用它,感谢 Ogglas

Update 2019-08-09 to avoid problem with refresh use this instead, thanks to Ogglas

<Redirect exact from="/" to="searchDashboard" />

这篇关于如何在 React Router 中将 DefaultRoute 设置为另一个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆