Pos*_*Guy 5 javascript reactjs react-router redux react-redux
我可能缺少历史记录或其他东西,但当我刷新子路线上的页面时,例如/login我得到的任何其他路线:
403禁止
代码:AccessDenied消息:访问被拒绝RequestId:075CAA73BDC6F1B9 HostId:O1n36xVCoeu/aLaSMrtCkAFZruWk5ZcO4RIrznEhvUxdu8lFEhL0XcKw2L4W4J7VYYet + HDv8tc =
请记住,只要我不刷新,路线就可以正常工作.
所以我想去/(主页).在它上面,我有一个超链接映射到/login我的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>
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
现在我遗漏了一堆重复的代码,这基本上显示我在这里唯一的路由是,如果他们成功登录,他们被重定向到/dashboard.问题不在于,重定向工作正常.无论何时我在我正在打开的任何页面上刷新,它都不会再次重新路由.
客户端和服务器端路由之间存在差异。这已在 squillion 帖子和其他地方进行了介绍。基本上,如果您的客户端反应应用程序将它们重定向到某个路由,这与它们在浏览器中手动输入该路由(它联系服务器并尝试获取该特定路由)不同。
因此,如果您在前端有一条路线,请说:
<Route component={Potato} exact path='/potato' />
Run Code Online (Sandbox Code Playgroud)
但在你的后端,你没有处理对所述路径的请求的东西,然后手动转到该 URL 或在该 URL 上刷新将导致你的应用程序自行排便。
确保你的后端有一个包罗万象的路由,如下所示:
app.get('*', (request, response) => {
response.sendFile(path.resolve(__dirname, 'index.html'))
});
Run Code Online (Sandbox Code Playgroud)
本质上,一个包罗万象的路线,只返回你的index.html
另外,请务必查找客户端与服务器端路由,因为这会导致页面刷新和使用 React Router 手动导航到某些 URL 的问题
| 归档时间: |
|
| 查看次数: |
7824 次 |
| 最近记录: |