繁体   English   中英

如何在我的反应路由器代码中添加错误页面?

[英]How do i add an error page to my react router code?

所以这是我的代码

              <Router>
                <Route path="/" component={Nav}/>
                <Route path="/" exact component={Home}/>
                <Route path="/updates" exact component={Updates} />
                <Route path="/author" exact component={Author} />
                <Route path="/" component={Footer}/>
              </Router>

它工作得很好,我只是有一个小问题。 如果我要 go 让我们说作者页面,我可以输入/author并且它不仅会显示作者,还会显示导航栏和页脚,因为我没有故意使它们精确,但问题是如果我要随机./ejfjife之类的链接它仍然会显示我想要制作的导航栏和页脚,因此如果用户制作了一个不存在的链接,它会将他们重定向到某种“此页面不存在”错误

谢谢你

如果你总是希望你的 Nav 和 Footer 出现,它们甚至不应该被列在Route下。 它们可以存在于您的Router之外。 然后,您的Home组件可以采用path="/" exact的路径,并且任何其他路由都可以采用path="/" Component={Error404} 像这样:

<App>

  <Nav />

  <Router>
    <Route path="/" exact component={Home}/>
    <Route path="/updates" exact component={Updates} />
    <Route path="/author" exact component={Author} />
    <Route path="/" component={Error404}/>
  </Router>

  <Footer />

</App>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM