登录  /  注册
React Router v6:无法正确捕获所有路径中的“*”字符问题解决方案
P粉579008412
P粉579008412 2023-08-25 19:28:54
[React讨论组]
<p>我有以下两个文件</p> <p><strong>AppRoutes.tsx</strong></p> <pre class="brush:php;toolbar:false;">import { Route, Routes } from "react-router-dom"; import NotFound from "../pages/NotFound"; import MessageRoutes from "../features/messages/routes/MessageRoutes"; import Home from "../pages/Home"; export default function AppRoutes() { return ( &lt;Routes&gt; &lt;Route path="/" element={&lt;Home /&gt;} /&gt; &lt;Route path="/messages/*" element={&lt;MessageRoutes /&gt;} /&gt; &lt;Route path="*" element={&lt;NotFound /&gt;} /&gt; &lt;/Routes&gt; ); }</pre> <p><strong>MessageRoutes.tsx</strong></p> <pre class="brush:php;toolbar:false;">import { Route, Routes } from "react-router-dom"; import ProtectedRoutes from "../../../routes/ProtectedRoutes"; import MessageOverview from "../pages/MessageOverview"; import NewMessage from "../pages/NewMessage"; export default function MessageRoutes() { return ( &lt;Routes&gt; &lt;Route element={&lt;ProtectedRoutes /&gt;}&gt; &lt;Route path="/" element={&lt;MessageOverview /&gt;} /&gt; &lt;Route path="/new" element={&lt;NewMessage /&gt;} /&gt; &lt;/Route&gt; &lt;/Routes&gt; ); }</pre> <p>因为我使用路径 "/messages/*" 来捕获以 /messages 开头的所有路径,所以我的 MessageRoutes 组件负责处理这些嵌套路由。我在 AppRoutes 组件中有一个最终的 "*" 路由来捕获应用程序不支持的任何 URL。但是如果路径是 "/messages/loremipsum",react router 不会捕获 NotFound 路由,因为以 "/messages" 开头的所有内容都将由 MessageRoutes 组件处理。</p> <p>这是否意味着在每个嵌套路由组件中,我现在都必须再次添加一个最终的 <code><route path="*" element="{<NotFound" ="">} /&gt;</route></code>,以支持最终的捕获所有路由?我不喜欢这种方法。难道没有绝对的最终捕获所有路由的方法吗?</p>
P粉579008412
P粉579008412

全部回复(1)
P粉458913655

是的,绝对可以。每个Routes组件都管理着自己可以匹配的路由的“范围”。例如,如果当前的URL路径是"/messages/loremipsum",根Routes组件会匹配"/messages/*",并正确渲染MessageRoutes组件。然后,MessageRoutes组件的Routes组件会继续匹配下一个路径段。由于没有"*/loremipsum"的路由路径,您需要另一个“catch-all”路由来处理这个。

问题在于Routes组件不知道它的任何路由可能渲染的后代路由。

示例:

import { Route, Routes } from "react-router-dom";
import NotFound from "../pages/NotFound";
import MessageRoutes from "../features/messages/routes/MessageRoutes";
import Home from "../pages/Home";

export default function AppRoutes() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/messages/*" element={<MessageRoutes />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}
import { Route, Routes } from "react-router-dom";
import ProtectedRoutes from "../../../routes/ProtectedRoutes";
import MessageOverview from "../pages/MessageOverview";
import NewMessage from "../pages/NewMessage";
import NotFound from "../pages/NotFound";

export default function MessageRoutes() {
  return (
    <Routes>
      <Route element={<ProtectedRoutes />}>
        <Route path="/" element={<MessageOverview />} />
        <Route path="/new" element={<NewMessage />} />
        <Route path="*" element={<NotFound />} />
      </Route>
    </Routes>
  );
}

如果您想要一个单独的“catch-all”路由,那么您需要一个单独的路由配置。

示例:

import { Route, Routes } from "react-router-dom";
import MessageOverview from "../pages/MessageOverview";
import NewMessage from "../pages/NewMessage";
import NotFound from "../pages/NotFound";
import Home from "../pages/Home";

export default function AppRoutes() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route element={<ProtectedRoutes />}>
        <Route path="/messages">
          <Route index element={<MessageOverview />} />
          <Route path="new" element={<NewMessage />} />
        </Route>
      </Route>
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}

现在,当URL路径是"/messages/loremipsum"时,这个Routes组件知道它正在渲染的嵌套路由,并且可以正确匹配并渲染NotFound

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号