reactjs - `window.location.href` 在 github 页面上部署时不适用于 react 应用程序
问题描述
有一个类似的问题,但我的问题是不同的。
在 localhost 上运行react
应用程序时,它会从以下页面导航,
提交按钮有代码window.location.href = "/ahana-psychometry/assessments/";
所以它导航到页面:
但是当我按下 gh-pages 页面上的提交按钮时,url 会从blenderous.github.io/ahana-psychometry/create
变为blenderous.github.io/ahana-psychometry/assessments/
. 但
但页面显示的是 404 消息:
解决方案
当您使用反应路由器时,最好的方法是使用它的方法。
这是一个示例,描述了使用 withRouter HOC 重定向到另一个页面:
import {
withRouter
} from 'react-router-dom'
class Sample extends React.Component {
handleSubmit = (e) => {
...
this.props.history.push('/ahana-psychometry/')
}
render() {
return (
<div>
<h1>Form</h1>
<Form onSubmit={this.handleSubmit} />
</div>
)
}
}
export default withRouter(Sample)
推荐阅读
- tornado - 如何在龙卷风的ioloop中获取待处理任务的数量?
- python - 为什么使用 str.format() 时会给出两个不同的结果?
- python - Django:为类别/外键中的对象设置视图、网址和模板
- lokijs - 是否可以获得 lokijs 数据库内容的散列(例如 SHA256 散列)?
- node.js - 登录后如何将购物车项目移动到新会话?
- python - Django - 如何将 getattr 用于动态模型?
- python - 在 matplotlib 中将图形大小设置为大于屏幕大小
- mysql - vb.net 中的访问被拒绝
- android - 如何在android中创建自定义弹出菜单设计
- javascript - 在 promise 返回方法输出中包装一个 promise