首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以同时拥有(例如) /posts和/posts/:id吗?

可以同时拥有(例如) /posts和/posts/:id吗?
EN

Stack Overflow用户
提问于 2019-09-29 08:50:03
回答 1查看 27关注 0票数 0

我正在使用react-router,我想知道如何才能同时拥有x.com/post和x.com/post/3这样的路由

在/posts路径中,我希望显示所有帖子列表,在/posts/3中,我希望显示帖子的详细信息

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-29 09:10:00

是!这是可能的。请从react router docs查看此说明

代码语言:javascript
复制
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>
  );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58151353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档