【问题标题】:Page redirects to index.html after on submit when deployed in AWS ampify在 AWS amplify 中部署时,页面在提交后重定向到 index.html
【发布时间】:2019-07-13 08:29:07
【问题描述】:

我已经在 AWS amplify 中部署了一个 react 应用程序。它在本地主机上运行良好,但是部署后,react 路由器似乎没有按预期工作。即当手动输入链接时,它重定向到 index.html 页面。此外,虽然我提到了event.preventDefault(),但在提交表单时,页面重定向到index.html

App.js

return (
    <Router>
    <div className="App">

    <Switch>



    <Route exact path="/SignUp" component={SignVerification}/>

    <Route exact path="/" component={()=><Home BlogInfo={this.state.BlogInfo} onAddUpvote={this.addUpvote} onAddPost={this.addPost}/>} />

        <Route exact path="/One/:id" render={(params)=>(<Single BlogInfo={this.state.BlogInfo}{...params} />)}/>


  </Switch>


    </div>

    </Router>
  );
}
}

Post.js

  handleSubmit=(event)=>
{

  axios.post("https://3em19ts012.execute-api.us-east-2.amazonaws.com/Dev/comment",

    {Comment :this.state.comments,
    PostId:this.props.match.params.id,
    Time:new Date(),
    Username:userPool.getCurrentUser().getUsername(),
    ParentId:"None"
    }      

).then((response) => {
  var data={comment:response.data.message.Comt.S,
    Username:response.data.message.Username.S
    };
    this.setState({commentInfo:[data,...this.state.commentInfo]})

  });









  event.preventDefault();


this.setState({comments:""})



}

【问题讨论】:

    标签: reactjs react-router aws-amplify


    【解决方案1】:

    似乎 AWS 在收到导致 404 的请求时将重定向(302)放大到 index.html。

    我能想到2个解决方案:

    1. 使用 HashRouter 而不是 BrowserRouter。或
    2. 配置 AWS 放大以在收到 404 时发送 200 redirects (rewrites)

    【讨论】:

    • 有什么办法可以防止页面重新加载
    • 在提交表单时,您可以防止页面重新加载。请粘贴您在表单提交上调用的方法。
    • 我上传了on form submit方法,在本地可以正常使用
    • 它应该可以工作。是否可以获得托管应用程序的链接?
    • master.df4818z86d6ru.amplifyapp.com/#/home 转到热门帖子并单击这些照片的任何标题并尝试添加评论
    猜你喜欢
    • 2016-01-09
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多