【问题标题】:react router page not able to refresh反应路由器页面无法刷新
【发布时间】:2019-01-12 10:08:42
【问题描述】:

我想尝试嵌套路由,但它不起作用

Codesandbox Link

场景: 当我第一次在 /category/bags 上路由时,它正在正确呈现 但是当我在此页面上刷新时,它给了我错误页面 Not found

我尝试使用 this.props.match.url 和 this.props.match.path 但在这里我收到错误,就像没有 prop ma​​tch.url

我尝试了以下链接

https://reacttraining.com/react-router/web/guides/quick-start

How to include the Match object into a ReactJs component class?

Nested routes with react router v4

【问题讨论】:

  • 看看我的回答,如果有帮助,请将其标记为已接受。

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


【解决方案1】:

我看过你的代码,发现你在父路由中使用了exact:

<Route exact path="/category" component={Category} />

如果您从父路由中删除确切的路由,那么它将正常工作:

<Route path="/category" component={Category} />

【讨论】:

  • 它工作了,谢谢..但确切用于确保正确的路线被渲染,而不是全部存在于给定的路线中,例如/category 。你能解释为什么它不起作用吗?如果路由 /category/bags 第一次渲染,那么它也应该在刷新时渲染
  • 这就是 react router 的工作方式,要了解更多信息,您可以参考以下博客 sitepoint.com/react-router-v4-complete-guide
  • 很高兴听到它对您有帮助,请接受答案。
【解决方案2】:

尝试使用ConnectedRouter from ‘react-router-redux’ 而不是BrowserRouter from 'react-router-dom'

这篇文章希望对你有帮助:

https://medium.com/@Dragonza/react-router-problem-with-gh-pages-c93a5e243819

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 2018-02-15
    • 1970-01-01
    • 2020-09-01
    • 2018-08-17
    • 2021-12-31
    • 2016-03-07
    • 2021-11-19
    • 2017-08-27
    相关资源
    最近更新 更多