我正在使用react-router,我想知道如何才能同时拥有x.com/post和x.com/post/3这样的路由
在/posts路径中,我希望显示所有帖子列表,在/posts/3中,我希望显示帖子的详细信息
发布于 2019-09-29 09:10:00
是!这是可能的。请从react router docs查看此说明
function App() {
return (
<div>
<Switch>
{/* If the current URL is /about, this route is rendered
while the rest are ignored */}
<Route path="/about">
<About />
</Route>
{/* Note how these two routes are ordered. The more specific
path="/contact/:id" comes before path="/contact" so that
route will render when viewing an individual contact */}
<Route path="/contact/:id">
<Contact />
</Route>
<Route path="/contact">
<AllContacts />
</Route>
{/* If none of the previous routes render anything,
this route acts as a fallback.
Important: A route with path="/" will *always* match
the URL because all URLs begin with a /. So that's
why we put this one last of all */}
<Route path="/">
<Home />
</Route>
</Switch>
</div>
);
}https://stackoverflow.com/questions/58151353
复制相似问题