Возможно ли иметь оба (например) / posts и / posts /: id? - PullRequest
0 голосов
/ 29 сентября 2019

Я использую реагирующий маршрутизатор и хочу знать, как можно использовать оба маршрута, например: x.com/posts и x.com/posts/3

в маршруте / postsЯ хочу показать весь список сообщений, а в / posts / 3 я хочу показать подробности сообщения

1 Ответ

1 голос
/ 29 сентября 2019

Да!это возможно.проверить это объяснение от реагировать на документы маршрутизатора :

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>
  );
}
...