【问题标题】:Implementing relative paths on github.io/project in React在 React 中实现 github.io/project 上的相对路径
【发布时间】:2019-07-24 14:44:01
【问题描述】:

当我收到错误时,可以使用 React Router 在 React 中为 gh-pages 实现 https://name.github.io/project 上的相对路径。 我使用import {withRouter} from 'react-router-dom';export default withRouter(componentName); 将路由道具从路由器转发到目标组件,然后我收到了它们。 我试过这样实现链接:

<li><Link to={{pathname: this.props.match.url + '/new-post'}}>New Post</Link></li>

并使用this.props.match.url这种方式获取当前路径,但我收到错误。

大家有什么建议吗?

【问题讨论】:

  • 你有匹配的吗?
  • @mhatch 匹配的 是什么意思?我是否在 中实现了相同的:路径名:this.props.match.url + '/new-post'?

标签: reactjs


【解决方案1】:

可以在react路由历史中配置:

history.js

import createBrowserHistory from 'history/createBrowserHistory';

const history = createBrowserHistory({
  basename: 'project',
});

export default history;

【讨论】:

  • 我应该把 REACT_APP_TEST_HOMEPAGE=name.github.io/project 放在哪里?我应该在哪里导入history.js,我想在链接所在的组件中?
  • 你不需要使用环境,你可以使用一个字符串,我个人在一个创建反应应用程序和我的环境中做到了,.env``. history` 是核心库 react-router用于处理历史。你应该阅读browserHistory,`hashHistory,memoryHistory`。历史记录应该传递给您的路由器/交换机
  • 你有学习browserHistory和hashHistory的资源吗?我在你的管子上发现了这个? youtube.com/watch?v=cdUyEou0LHg
  • historyreact-router 文档呢?
  • 总而言之,你需要一个为基本路径/project配置的路由器,这意味着一个应该带到/project/test。设置base时不需要设置完整路径。见stackoverflow.com/questions/38196448/…
猜你喜欢
  • 2016-08-19
  • 2011-10-17
  • 2016-11-09
  • 1970-01-01
  • 2014-01-17
  • 2020-01-08
  • 1970-01-01
  • 2012-08-24
  • 2019-06-28
相关资源
最近更新 更多