javascript – React-router v4页面刷新不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – React-router v4页面刷新不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2299字,纯文字阅读大概需要4分钟。
内容图文
![javascript – React-router v4页面刷新不起作用](/upload/InfoBanner/zyjiaocheng/820/a25736f3cfae4ecc8df8ca6b8a844a3a.jpg)
我可能错过了历史记录或其他什么,但当我刷新子路径上的页面,如/ login或任何其他路线,我得到:
403禁止
代码:AccessDenied
消息:访问被拒绝
RequestId:075CAA73BDC6F1B9
HostId:O1n36xVCoeu / aLaSMrtCkAFZruWk5ZcO4RIrznEhvUxdu8lFEhL0XcKw2L4W4J7VYYet HDv8tc =
请记住,只要我不刷新,路线就可以正常工作.
所以我想去/(主页).在它上面,我有一个超链接,映射到我的react-router路由中的路径/登录.我单击超链接,它工作正常,根据我的react-router路由呈现/ login控件.只有当我刷新任何页面时,我才会得到上面的错误.
此外,当我同步我的存储桶时,我正在对其进行公开读取,因此不确定为什么我收到此权限错误:
aws s3 sync –acl public-read build s3:// ${bkt}
以下是一些路线和组件的示例:
<Provider store={store}>
<Layout>
<Switch>
<Route component={Home} exact path='/' />
<ProtectedRoute component={
DashboardContainer} path='/dashboard' />
<Route component={LoginContainer} path='/login' />
<Route component={NotFound} path='*' />
</Switch>
</Layout>
</Provider>
LoginContainer
import { connect } from 'react-redux'
import React, { Component } from 'react'
const _ = require('lodash')
...
import Login from '../components/auth/Login/Login'
class LoginContainer extends Component {
constructor(props) {
super(props)
this.handleEmailInput = this.handleEmailInput.bind(this)
... rest of the handlers
}
async handleLoginPressed(e) {
e.preventDefault()
this.validateForm()
await this.props.authenticate(this.state.email, this.state.password)
if(this.props.isAuthenticated) {
this.props.history.push('/dashboard')
return
}
... more code
render(){
return(
<div>
<Login
login={this.handleLoginPressed}
/>
</div>
)
}
}
const mapStateToProps = state => ({
token: state.auth.token
})
export const mapDispatchToProps = {
authenticate: AuthAsyncActions.authenticate
}
export { Login }
export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer)
现在我遗漏了一堆重复的代码,这基本上显示我在这里唯一的路由是,如果他们成功登录,他们被重定向到/仪表板.问题不在于,重定向工作正常.无论何时我在我正在打开的任何页面上刷新,它都不会再次重新路由.
解决方法:
这是因为您的服务器尝试直接转到该URL.您有两种选择:
>使用react-router v4提供的hash router.路线现在看起来像这样:http://example.com/#/somePage
>如果您不想在URL上添加#,则必须更改服务器行为.对于不同的服务器,此配置会发生变化.在开发模式下,如果您使用的是webpack-dev-server,则只需启用historyApiFallback.在生产模式下,搜索如何使用您将使用的服务器重定向403/404响应.
内容总结
以上是互联网集市为您收集整理的javascript – React-router v4页面刷新不起作用全部内容,希望文章能够帮你解决javascript – React-router v4页面刷新不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。