在嵌套的React路由中使用搜索参数时,当我在/app上,点击导航到/app/user的链接后,导航不起作用。
如果我尝试不嵌套使用,它是可以工作的。但为什么在嵌套时它不起作用。
Codesandbox: CodeSandBox链接
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/app" element={<Main />}>
<Route path=":user" element={<User />} />
</Route>
<Route path="*" element={<PageNotFound />} />
</Routes> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
尝试一下
<Routes> <Route path="/" element={<LandingPage />} /> <Route path="/app" element={<Main />} /> <Route path="/app/:user" element={<User />} /> <Route path="*" element={<PageNotFound />} /> </Routes>如果你想将
User.js组件嵌套,你需要在Main.js中添加Outletimport { Link, Outlet } from "react-router-dom"; export default function Main() { return ( <div> <p>Main Page</p> <Link to="/app/ashish">点击进入用户页面</Link> <Outlet/> </div> ); }