【问题标题】:Displaying PageNotFound component in every page. React在每个页面中显示 PageNotFound 组件。反应
【发布时间】:2021-11-24 00:01:19
【问题描述】:

我不明白为什么我的 PageNotFound 组件会显示在我的应用程序中,即使我已将它放在我所有路线的底部?问题可能是嵌套路由,但我仍然不知道该怎么做。非常感谢您的帮助,这是我的代码:

    <>
  <BrowserRouter>
    <>
      <div className="content-wrap">
        <div className="main-content">
          <Menu />
          <main className="main-pages">
            <Switch>
              <Container>
                {/* admin */}
                {userInfo && userInfo.isAdmin && (
                  <>
                    <PrivateRoute
                      path="/admin/userlist"
                      component={UserList}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/userlist/:pageNumber"
                      component={UserList}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/user/:id/edit"
                      component={UserEdit}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/createuser"
                      component={UserCreate}
                    />
                    <PrivateRoute
                      path="/admin/user/:id/contact"
                      component={UserContact}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/productlist"
                      component={ProductList}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/productlist/:pageNumber"
                      component={ProductList}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/product/:id/edit"
                      component={ProductEdit}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/orderlist"
                      component={OrderList}
                      exact
                    />
                    <PrivateRoute
                      path="/admin/orderlist/:pageNumber"
                      component={OrderList}
                      exact
                    />
                  </>
                )}

                {/* private */}
                <PrivateRoute path="/profile" component={Profile} />
                <PrivateRoute path="/shipping" component={Shipping} />
                <PrivateRoute path="/payment" component={Payment} />
                <PrivateRoute path="/placeorder" component={PlaceOrder} />
                <PrivateRoute path="/order/:id" component={Order} />

                {/* public */}
                <Route path="/products/:id" component={ProductDetail} />
                <Route path="/about" component={About} />
                <Route path="/cart/:id?" component={Cart} />
                <Route path="/login" component={Login} />
                <Route path="/register" component={Register} />
                <Route exact path="/search/:word" component={Home} />
                <Route
                  exact
                  path="/search/:word/page/:pageNumber"
                  component={Home}
                />
                <Route exact path="/page/:pageNumber" component={Home}/>
                <Route exact path="/" component={Home} />
                <Route component={PageNotFound}/> 
              </Container>
            </Switch>
          </main>
        </div>
        <Footer />
      </div>
    </>
  </BrowserRouter>

我尝试过更改订单等等,但仍然没有用..

【问题讨论】:

    标签: reactjs react-redux routes react-router


    【解决方案1】:

    试试看: &lt;Route path="*" component={PageNotFound}/&gt;

    【讨论】:

    • 我试过了,没用:/
    • 尝试把那个从Switch中取出来,这样Switch组件就只有路由组件了。
    • 无论如何都没有工作 thx :)
    猜你喜欢
    • 2021-07-27
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    相关资源
    最近更新 更多