【问题标题】:Redirect '/' to a specific route in react-router 4 at app start在应用程序启动时将“/”重定向到 react-router 4 中的特定路由
【发布时间】:2018-07-04 17:45:04
【问题描述】:

我正在尝试在应用程序启动时将我的应用程序重定向到特定路线。我试过这个

 <Route exact path="/todos" component={Todo} />
 <Route exact path="/users" component={User} />
 <Redirect path="/" to="/todos" />

但它有两个问题

  1. 它在刷新时将我重定向到 todos 路由
  2. 它给了我以下警告

You tried to redirect to the same route you're currently on: "/todos"

我想像我们一样在 Angular 中实现重定向概念:

{ path: '', redirectTo: '/heroes', pathMatch: 'full' },

【问题讨论】:

    标签: angularjs reactjs redirect react-router


    【解决方案1】:

    您需要将它们包装在 &lt;Switch&gt; 中,例如:

    <Switch>
     <Route exact path="/todos" component={Todo} />
     <Route exact path="/users" component={User} />
     <Redirect from="/" to="/todos" />
    </Switch>
    

    它将按顺序匹配路线。例如:如果你有/todos,它将匹配第一个,如果你有/users,它将匹配第二个,对于“/”,它将重定向到“/todos”

    Redirect 还有 fromto 属性。你的例子有path 而不是from

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多