我想将React路线代码从V5转换为V6

发布于 2025-02-11 05:54:18 字数 1399 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2025-02-18 05:54:18

这是您在app组件中需要的代码,但是您也可能需要对其他组件应用一些更改。 (请查看下面的链接)

function App() {
  
  return (
    
 

 <div>
    <h2>JWT Authentication using JSON fake server</h2>
    <Routes>
      <Route path="/" element={<> <Header/> <Home /></>}/>
      <Route path="/login" element={<Login  />}/>
      <Route path="/register" element={ <Register />}/>
    </Routes>
  </div>



 );
}

export default App;

首先,您必须知道switchexact在V6中不存在。
您需要在V6中导入 而不是switch

至于精确,在v6中,默认匹配仅在整个路径是匹配的,而不仅仅是开始(这是V5中的行为)。因此,在V6中,您无需添加精确。

如果您确实想要它导航的行为,即使只是路径的开始匹配,也可以在路径末端添加/*

查看此链接以获取有关更改的更多信息: https> https:htttps //reactrouter.com/docs/en/v6/upgrading/v5

This is the code you need in the App component , but you may also need to apply some changes on other components. (check out the link below)

function App() {
  
  return (
    
 

 <div>
    <h2>JWT Authentication using JSON fake server</h2>
    <Routes>
      <Route path="/" element={<> <Header/> <Home /></>}/>
      <Route path="/login" element={<Login  />}/>
      <Route path="/register" element={ <Register />}/>
    </Routes>
  </div>



 );
}

export default App;

First you must know that Switch and exact doesn't exist in v6 anymore.
Instead of Switch you need to import Routes in v6.

As for exact, in v6 the matching by default is only if the whole path is matching and not just the begining (which is the behavior in v5.) So in v6 you don't need to add exact.

If you do want that behavior that it navigates even if just the begining of the path matches, you can add /* at the end of the path.

Check out this link for more information about the changes: https://reactrouter.com/docs/en/v6/upgrading/v5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文