【问题标题】:After adding react-router-dom my github pages showing nothing添加 react-router-dom 后,我的 github 页面什么也没有显示
【发布时间】:2020-04-13 10:55:47
【问题描述】:

添加 react-router-dom 后,我将其推送到 GitHub https://github.com/iamtheasad/user。在项目 url 中,我的主页显示 https://iamtheasad.github.io/user/,之后我单击另一个菜单项,它重定向到另一个页面,但是当我刷新另一个页面时,它显示 404 页面,单击主页,它把我带到了这个链接 https://iamtheasad.github.io/,它是给我看空白页。

我的导航组件链接:https://github.com/iamtheasad/user/tree/master/src/components/TopNavigation

我的路由器组件链接:https://github.com/iamtheasad/user/tree/master/src/router

我正在尝试添加这些:<NavLink exact activeStyle={{color: '#00a8ee'}} className={this.state.navBarItem} to={process.env.PUBLIC_URL + '/'}>Home</NavLink> </Nav.Link>

<Route exact path={process.env.PUBLIC_URL + '/'} component={HomePage}/>
                <Route exact path="/service" component={ServicePage}/>
                <Route exact path="/course" component={CoursesPage}/>
                <Route exact path="/portfolio" component={PortfolioPage}/>
                <Route exact path="/contact" component={ContactPage}/>
                <Route exact path="/about" component={AboutPage}/>

【问题讨论】:

  • 尝试从to 组件中的to 属性中删除process.env.PUBLIC_URL,据我所知链接是相对的
  • 我刚试过。它没有用。
  • 你需要展示你的路由文件的完整代码,你正在渲染路由组件。
  • 这是我的路由器文件链接https://github.com/iamtheasad/user/blob/master/src/router/AppRoute.js

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


【解决方案1】:

尝试在房间级别的package.json 文件中添加homepage 属性。

package.json

{
    "homepage": "https://iamtheasad.github.io/user"
}

user 是存储库的名称

【讨论】:

【解决方案2】:

react-router 是一个客户端路由器。要解决此问题,您需要修复服务器基础路由器。当用户访问像 www.user.github.io/repo 这样的 URL 时,github 将尝试将 github 页面存储库中的 repo/index.html 提供给用户。如果该文件不存在,那么它们将返回一个 404.html 页面,并且它们还为您提供了一种拥有自定义 404 页面的方法。

【讨论】:

  • 那我现在该怎么办?
  • 您的方法要求您在服务器上启用 pdf worker。使用此链接中基本用法下的示例。 github.com/wojtekmaj/react-pdf
猜你喜欢
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2022-11-20
  • 1970-01-01
  • 2022-09-23
  • 2018-07-07
  • 2021-06-12
相关资源
最近更新 更多