将根路径重定向到React Router 6中的子路径

huangapple go评论63阅读模式
英文:

Redirect root path to child in react-router 6

问题

我试图强制将根路径/重定向到子路径(我们有多个模块,根路径只会将您重定向到第一个模块,实际根路径上没有内容可显示)。

但是,我找不到任何关于根路径的示例,并尝试其他方法(使用导航到会触发无限循环)。

<Route element={<Navigate replace to='/module/submodule' />} path='/' />
  <Route element={<Module />} handle={{ crumb: (): string => 'Module' }} path='module'>
    <Route element={<Submodule />} handle={{ crumb: (): string => 'Submodule' }} path='submodule' />
  </Route>
</Route>

我需要将/重定向到/module/submodule

英文:

I'm trying to force redirect the exact root path / to a child path (we have several modules and the root will just direct you to the first one, there's nothing to show at the actual root).

However I can't find any examples for the root path, and trying other approaches (with Navigation to just trigger an infinite loop.

&lt;Route element={&lt;Navigate replace to=&#39;/module/submodule&#39; /&gt;} path=&#39;/&#39;&gt;
  &lt;Route element={&lt;Module /&gt;} handle={{ crumb: (): string =&gt; &#39;Module&#39; }} path=&#39;module&#39;&gt;
    &lt;Route element={&lt;Submodule /&gt;} handle={{ crumb: (): string =&gt; &#39;Submodule&#39; }} path=&#39;submodule&#39; /&gt;
  &lt;/Route&gt;
&lt;/Route&gt;

I need to redirect / to /module/submodule

答案1

得分: 3

你应该能够使用索引路由。

<Route path='/'>
  <Route element={<Navigate replace to='/module/submodule' />} index />
  <Route element={<Module />} handle={{ crumb: (): string => 'Module' }} path='module'>
    <Route element={<Submodule />} handle={{ crumb: (): string => 'Submodule' }} path='submodule' />
  </Route>
</Route>
英文:

You should be able to use an index route.

&lt;Route path=&#39;/&#39;&gt;
  &lt;Route element={&lt;Navigate replace to=&#39;/module/submodule&#39; /&gt;} index /&gt;
  &lt;Route element={&lt;Module /&gt;} handle={{ crumb: (): string =&gt; &#39;Module&#39; }} path=&#39;module&#39;&gt;
    &lt;Route element={&lt;Submodule /&gt;} handle={{ crumb: (): string =&gt; &#39;Submodule&#39; }} path=&#39;submodule&#39; /&gt;
  &lt;/Route&gt;
&lt;/Route&gt;

答案2

得分: 1

将重定向移到嵌套的“catch-all”路由中。任何未匹配任何路由的内容都将重定向到 "/module/submodule"

<Route path="/">
  <Route
    path="module"
    element={<Module />}
    handle={{ crumb: (): string => 'Module' }}
  >
    <Route
      path="submodule"
      element={<Submodule />}
      handle={{ crumb: (): string => 'Submodule' }}
    />
  </Route>
  <Route path="*" element={<Navigate replace to="/module/submodule" />} />
</Route>
英文:

Move the redirect into a nested "catch-all" route. Anything not matched by any route will be redirected to &quot;/module/submodule&quot;.

&lt;Route path=&quot;/&quot;&gt;
  &lt;Route
    path=&quot;module&quot;
    element={&lt;Module /&gt;}
    handle={{ crumb: (): string =&gt; &#39;Module&#39; }}
  &gt;
    &lt;Route
      path=&quot;submodule&quot;
      element={&lt;Submodule /&gt;}
      handle={{ crumb: (): string =&gt; &#39;Submodule&#39; }}
    /&gt;
  &lt;/Route&gt;
  &lt;Route path=&quot;*&quot; element={&lt;Navigate replace to=&quot;/module/submodule&quot; /&gt;} /&gt;
&lt;/Route&gt;

答案3

得分: 1

这是正确的 react-router-dom:6.15.0 用法吗?

英文:

Is this correct for react-router-dom:6.15.0?

const router = createBrowserRouter([
    {
        path: &#39;/&#39;,
        element: &lt;App /&gt;,
        errorElement: &lt;ErrorPage /&gt;,
        children: [
            {
                index: true,
                element: &lt;Navigate replace to=&#39;/projects&#39; /&gt;,
            },
            {
                path: &#39;/projects&#39;,
                element: &lt;ProjectsPage /&gt;,
            },
        ],
    },
])

huangapple
  • 本文由 发表于 2023年2月6日 07:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定