【问题标题】:Refresh in react page redirect it to /404 page在反应页面中刷新将其重定向到 /404 页面
【发布时间】:2021-02-21 10:35:12
【问题描述】:

我的反应应用程序中有一些路由,我描述了我的路由中的任何 url 重定向到 /404 页面,但它不能正常工作; 当我刷新我的页面时,它会重定向到 /404 页面,即使我想去 /(主页)重定向到 /404 到!以下是我的路线:

<Router>
      <Switch>
          <UserProvider>
             <PageLayout>
                <Route exact to="/" ><HomePage /></Route >
                <Route to="/product" ><Product /></Route >
                <Route to="/contactus" ><ContactUs /></Route >
                <Route to="/aboutus" ><AboutUs /></Route >
                <Route to="/404" ><NotFound /></Route >
                <Redirect from="/**/" to="/404" />
             </PageLayout >
          </UserProvider >
      </Switch>
</Router>

谁能告诉我我的问题是什么?

【问题讨论】:

    标签: reactjs react-router react-router-dom


    【解决方案1】:
    <Router>
      <Switch>
        <Route to="/" exact component={HomePage}/>
        <Route to="/product" exact component={Product}/>
        <Route to="/contactus" exact component={ContactUs}/>
        <Route to="/aboutus" exact component={AboutUs}/>
        <Route to="/*" exact component={NotFound}/>
      <Switch>
     <Router/>
    

    试试这个。我认为它会起作用;)

    【讨论】:

    • 感谢您的回答,但您的解决方案对我不起作用
    【解决方案2】:

    问题如下:

    <Router>
          <Switch>
            ...
            <Redirect from="/**/" to="/404" />
          </Switch>
    </Router>
    

    您将/**/ 重定向到/404

    阅读这篇文章here

    也许你应该这样做:

    将/**/ 更改为* 并将您的路线重新排列为:

    <Router>
          <Switch>
            ...
            <Redirect from="*" to="/404" />
            <Route to="/404" ><NotFound /></Route >
          </Switch>
    </Router>
    

    或者你可以只使用 (if React version >= 4)

    <Redirect to="/404" />
    

    React 路由器文档here

    【讨论】:

    • 感谢您的回答,我尝试了您的解决方案,但总的来说,它不再显示 404 页面
    • 我更新了我的答案。我忘了重新安排你的路线。这里的顺序很重要。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2018-02-07
    • 2016-02-28
    • 1970-01-01
    • 2019-11-27
    • 2012-10-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多