React Router не будет маршрутизировать до 404 компонента NotFoundPage - PullRequest
0 голосов
/ 10 апреля 2020

У меня настроен React router v4, как в приведенном ниже разделе кода.

<Router>
      <Switch>
          <Route exact path="/" component={HomePage}/>
          <Route>
              <Switch>
                  <Route exact path="/account" component={Account}/>
                  <Route exact path="/editProfile" component={Profile}/>
                  <Route exact path="/changePassword" component={Password}/>
                  <Route exact path="/testHtml" component={Pages}/>
                  <Route exact path="/audio" component={Develop}/>
                  <Route exact path="/content" component={Books}/>
              </Switch>
          </Route>
          <Route component={NotFoundPage}/>
      </Switch>
</Router>

Я хочу, чтобы мое приложение направляло к компоненту, когда какой-либо путь не совпадает, но не совпадает. В чем может быть проблема здесь ??

1 Ответ

0 голосов
/ 10 апреля 2020

Прежде всего, я бы удалил один из компонентов <Switch>, потому что вам нужен только один. Кроме того, если вы добавите компонент <Route> с <NotFoundPage> в последнее место в <Switch>, то он будет отображать, что один раз все остальные не могут совпадать.

Попробуйте выполнить следующее:

<Router>
    <Switch>
        <Route exact path="/" component={HomePage}/>
        <Route exact path="/account" component={Account}/>
        <Route exact path="/editProfile" component={Profile}/>
        <Route exact path="/changePassword" component={Password}/>
        <Route exact path="/testHtml" component={Pages}/>
        <Route exact path="/audio" component={Develop}/>
        <Route exact path="/content" component={Books}/>
        <Route component={NotFoundPage}/>
    </Switch>
</Router>

Надеюсь, это поможет!

...