【问题标题】:How to set the DefaultRoute to another Route in React Router如何在 React Router 中将 DefaultRoute 设置为另一个路由
【发布时间】:2015-06-15 15:37:53
【问题描述】:

我有以下:

  <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 中呈现。

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

基本上,如果用户转到主页,我想将他们发送到搜索仪表板。所以我想我正在寻找一个等同于window.location.replace("mygreathostname.com/#/dashboards/searchDashboard"); 的 React Router 功能

【问题讨论】:

  • 你试过用 Redirect 代替 DefaultRoute 吗?
  • @JonatanLundqvistMedén 这正是我想要的,谢谢!把它写成答案,我会把它标记为正确的。抱歉延迟回复。

标签: javascript routes reactjs url-redirection react-router


【解决方案1】:

您可以使用 Redirect 代替 DefaultRoute

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

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

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

【讨论】:

  • 只是我,或者当使用它直接点击深层网址时,我总是被重定向到“to”网址,而不是我试图点击的路线?
  • 应该注意,如果你正在做一些重定向,比如from='/a' to='/a/:id',你需要使用&lt;Switch&gt; 来包含来自react-router 的&lt;Redirect&gt; 和&lt;Route&gt; 组件。详情见doc
  • @Kulbear 我遇到了同样的问题。做奥格拉斯在他的回答中所说的工作是有效的。
  • 为了让它工作,你很可能需要把这条路线放在最后或者这样做&lt;Redirect exact from="/" to="/adaptation" /&gt;
  • 很容易监督,所以我重复一遍:DarkWalker 重定向规则的重要部分是exact 标志。接受的答案缺少那个,所以它匹配[几乎]任何路线。
【解决方案2】:

使用&lt;Redirect from="/" to="searchDashboard" /&gt; 的问题是,如果您有不同的网址,例如/indexDashboard,并且用户点击刷新或收到发送给他们的网址,用户将被重定向到/searchDashboard。

如果您不希望用户能够刷新站点或发送 URL,请使用:

<Route exact path="/" render={() => (
    <Redirect to="/searchDashboard"/>
)}/>

如果searchDashboard 在登录后使用这个:

<Route exact path="/" render={() => (
  loggedIn ? (
    <Redirect to="/searchDashboard"/>
  ) : (
    <Redirect to="/login"/>
  )
)}/>

【讨论】:

    【解决方案3】:

    我错误地尝试使用以下命令创建默认路径:

    <IndexRoute component={DefaultComponent} />
    <Route path="/default-path" component={DefaultComponent} />
    

    但这会创建两个不同的路径来渲染相同的组件。这不仅没有意义,而且会导致 UI 出现故障,即,当您根据 this.history.isActive() 设置 &lt;Link/&gt; 元素的样式时。

    创建默认路由(即不是索引路由)的正确方法是使用&lt;IndexRedirect/&gt;:

    <IndexRedirect to="/default-path" />
    <Route path="/default-path" component={DefaultComponent} />
    

    这是基于 react-router 1.0.0。见https://github.com/rackt/react-router/blob/master/modules/IndexRedirect.js。

    【讨论】:

    【解决方案4】:

    乔纳森的回答似乎对我不起作用。我正在使用 React v0.14.0 和 React Router v1.0.0-rc3。这样做了:

    &lt;IndexRoute component={Home}/&gt;.

    所以在马修的案例中,我相信他会想要:

    &lt;IndexRoute component={SearchDashboard}/&gt;.

    来源:https://github.com/rackt/react-router/blob/master/docs/guides/advanced/ComponentLifecycle.md

    【讨论】:

    • 感谢分享。我为此使用了 React v0.13 和 React-Router 版本,所以是 1.0/rc 之前的版本。希望这对其他人有帮助!
    • 我认为这会让你失去上下文。 SearchDashboard 将是您到达主页时将看到的组件,但如果您直接访问 /dashboard/searchDashboard,则不会是包装它的 Dashboard 组件。 React-router 根据 URL 匹配的路由动态构建嵌套组件的层次结构,所以我认为您确实需要在这里进行重定向。
    【解决方案5】:

    更新:2020

    不使用重定向,只需在path 中添加多个路由

    例子:

    <Route exact path={["/","/defaultPath"]} component={searchDashboard} />
    

    【讨论】:

    • 嘿,你确定那里有方括号吗?因为我收到错误:"Uncaught TypeError: meta.relativePath.startsWith is not a function" 所以当我删除方括号时,它工作正常,如下所示:&lt;Route exact path={"/","/defaultPath"} component={searchDashboard} /&gt;
    【解决方案6】:
    import { Route, Redirect } from "react-router-dom";
    
    class App extends Component {
      render() {
        return (
          <div>
            <Route path='/'>
              <Redirect to="/something" />
            </Route>
    //rest of code here
    

    这样当您在本地主机上加载服务器时,它会将您重定向到 /something

    【讨论】:

      【解决方案7】:

      我遇到了类似的问题;如果没有匹配的路由处理程序,我想要一个默认路由处理程序。

      我的解决方案是使用通配符作为路径值。 IE 还要确保它是您的路线定义中的最后一个条目。

      <Route path="/" component={App} >
          <IndexRoute component={HomePage} />
          <Route path="about" component={AboutPage} />
          <Route path="home" component={HomePage} />
          <Route path="*" component={HomePage} />
      </Route>
      

      【讨论】:

        【解决方案8】:

        对于即将进入 2017 年的用户,这是IndexRedirect 的新解决方案:

        <Route path="/" component={App}>
          <IndexRedirect to="/welcome" />
          <Route path="welcome" component={Welcome} />
          <Route path="about" component={About} />
        </Route>
        

        【讨论】:

          【解决方案9】:

          由于最近发布了 V6,因此接受的答案将不起作用,因为在 V6 中不再存在重定向。考虑使用导航。

           <Route path="/" element={<Navigate to="/searchDashboard" />} />
          

          参考:-V6 docs

          【讨论】:

            【解决方案10】:
             <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>
                <Redirect from="/*" to="/" />
              </Route>
            

            【讨论】:

            【解决方案11】:

            首选方法是使用react路由IndexRoutes组件

            你像这样使用它(取自上面链接的反应路由器文档):

            <Route path="/" component={App}>
                <IndexRedirect to="/welcome" />
                <Route path="welcome" component={Welcome} />
                <Route path="about" component={About} />
            </Route>
            

            【讨论】:

            • 链接页面现在给404
            【解决方案12】:

            首先你需要安装:

            npm install react-router-dom;
            

            然后您需要使用您的 App.js(在您的情况下可能会有所不同)并进行以下修改。 在这种情况下,我选择了重定向来获得正确的渲染过程。

            import { BrowserRouter as Router, Route, Switch, Redirect } from "react-router-dom";
            
            <Router>
                    <Suspense fallback={<Loading />}>
                      <Switch>
                        <Route exact path="/">
                          <Redirect to="/Home" component={Routes.HomePage}/>
                        </Route>
                        <Route exact path="/Biz" component={Routes.Biz} />
                      </Switch>
                    </Suspense>
                  </Router>
            

            你成功完成上面的修改你可以看到重定向 URL 在你的浏览器路径上,并且渲染过程也根据他们的组件正常工作。

            前段时间,我们有机会在 react 路由中使用了名为“DefaultRoute”的组件。

            现在,它已经过时了,而且使用起来也不是很流行,你可以创建名为 default 或其他名称的自定义路由,但在现代 React.js 开发中,这不是我们的做法。 em>

            只是因为使用“DefaultRoute”路由会导致一些渲染问题,这是我们绝对希望避免的。

            【讨论】:

              【解决方案13】:

              您可以像这样使用它来重定向特定的 URL,并在从旧路由器重定向到新路由器后渲染组件。

              <Route path="/old-router">
                <Redirect exact to="/new-router"/>
                <Route path="/new-router" component={NewRouterType}/>
              </Route>
              

              【讨论】:

                【解决方案14】:

                这是我的做法-

                <Router>
                  <div className="App">
                    <Navbar />
                    <TabBar />
                    <div className="content">
                      <Route exact path={["/default", "/"]}> //Imp
                        <DefStuff />
                      </Route>
                      <Route exact path="/otherpage">
                        <Otherstuff />
                      </Route>
                      <Redirect to="/defult" /> //Imp
                    </div>
                  </div>
                </Router>
                

                【讨论】:

                  猜你喜欢
                  • 2021-05-10
                  • 1970-01-01
                  • 2016-11-14
                  • 1970-01-01
                  • 2022-01-10
                  • 2016-01-14
                  • 2019-07-01
                  • 2016-07-09
                  • 2021-12-30
                  相关资源
                  最近更新 更多