简体   繁体   English

react router v6:如何设置绝对路径的路由?

[英]react router v6:how to set a route with absolute path?

if click the NavLink, go to /search, and Search will be displayed如果点击 NavLink,go 到 /search,就会显示 Search

how to solve this in router v6?如何在路由器 v6 中解决这个问题? help please!!!请帮忙!!!

function App() {
  return (
    <div>
      <BrowserRouter basename={'/index'}>
        <Routes>
          <Route path={'/A/*'} element={<Home/>} />
          <Route path={'/search/*'} element={<Home />} />
        </Routes>
      </BrowserRouter>

    </div>
  );
}

function Home() {
  return (
    <div>
      <div>Home</div>
      <NavLink to={'/search'}>Search</NavLink>
      <Routes>
        <Route path={'/search'} element={<Search/>}/>
        <Route path={'/B/B1/show'} element={<Show/>}/>
      </Routes>
      <div>Footer</div>
    </div>
  )
}

While React Router claims that absolute file paths still work in V6 on their website :虽然 React Router 声称绝对文件路径在其网站上的 V6 中仍然有效:

Note: Absolute paths still work in v6 to help make upgrading easier.注意:绝对路径在 v6 中仍然有效,以帮助简化升级。 You can even ignore relative paths altogether and keep using absolute paths forever if you'd like.如果您愿意,您甚至可以完全忽略相对路径并永远使用绝对路径。 We won't mind.我们不会介意的。

I'm not convinced it actually does for a subset of users who want this.我不相信它确实适用于想要这个的用户子集。

You can see the merge request for this functionality here but it explicitely calls out that this will only work if the child routes have the same path as the parent routes.您可以在此处查看此功能的合并请求,但它明确指出,这仅在子路由与父路由具有相同路径时才有效。

Child routes with absolute paths that do not match their parent routes are an error.绝对路径与其父路由不匹配的子路由是错误的。

Unfortunately for many of us, our child routes in websites using React Router pre-v6 don't have the same paths as their parent routes.不幸的是,对于我们中的许多人来说,我们在使用 React Router pre-v6 的网站中的子路由与它们的父路由没有相同的路径。

One possible solution is listed in this thread by user @jampy where they propose replacing the Routes component with a custom component that ignores the parent's path, thus allowing its Route components to use absolute paths.用户@jampy此线程中列出了一种可能的解决方案,他们建议将Routes组件替换为忽略父路径的自定义组件,从而允许其Route组件使用绝对路径。

The code you've shared already is using routes with absolute paths.您已经共享的代码正在使用具有绝对路径的路由。 The confusion seems to be with how links can use relative or absolute paths.混淆似乎在于链接如何使用相对或绝对路径。 The difference between relative and absolute paths is trivially simple: absolute paths start with a leading slash "/" character while relative paths do not.相对路径和绝对路径之间的区别非常简单:绝对路径以斜杠"/"字符开头,而相对路径则没有。

The NavLink component rendering a to="/search" prop is linking an absolute path to "/search" and the result is that the URL is updated to "/search" and the Home component continues to remain mounted and no other components or routes are matched and rendered.呈现to="/search"属性的NavLink组件将绝对路径链接到"/search" ,结果是 URL 更新为"/search" ,并且Home组件继续保持安装状态,没有其他组件或路由被匹配和渲染。

If the goal is to click the <NavLink to="search">Search</NavLink> link and navigate to "/A/search" then use a relative link path.如果目标是单击<NavLink to="search">Search</NavLink>链接并导航到"/A/search" ,则使用相对链接路径。 Remove the leading "/" from the link path target.从链接路径目标中删除前导"/"

function Home() {
  return (
    <div>
      <div>Home</div>
      <NavLink to="search">Search</NavLink> // link to "./search"
      <Routes>
        <Route path="/search" element={<Search />} />  // "/A/search" or "/search/search"
        <Route path="/B/B1/show" element={<Show />} /> // "/A/B/B1/show" or "/search/B/B1/show"
      </Routes>
      <div>Footer</div>
    </div>
  );
}

编辑 react-router-v6-how-to-set-a-route-with-absolute-path

Since Home is rendered on both "/A" and "/search" as a root route, it can link relative to "search" and "B/B1/show" from either root path.由于Home"/A""/search"上都呈现为根路径,因此它可以从任一根路径相对于"search""B/B1/show"链接。

Sandbox code to demo this behavior:演示此行为的沙箱代码:

function Home() {
  return (
    <div>
      <div>Home</div>
      <NavLink to="search">Search</NavLink>{" "}
      <NavLink to="B/B1/show">Show</NavLink>
      <Routes>
        <Route path="/search" element={<Search />} />
        <Route path="/B/B1/show" element={<Show />} />
      </Routes>
      <div>Footer</div>
    </div>
  );
}

export default function App() {
  return (
    <div className="App">
      <ul>
        <li>
          <NavLink to="/A">A</NavLink>
        </li>
        <li>
          <NavLink to="/search">Search</NavLink>
        </li>
      </ul>
      <Routes>
        <Route path="/A/*" element={<Home />} />
        <Route path="/search/*" element={<Home />} />
        <Route path="*" element={<Navigate to="/A" replace />} />
      </Routes>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM