【问题标题】:need to refresh page after axios callaxios调用后需要刷新页面
【发布时间】:2020-07-04 12:51:23
【问题描述】:

正如标题所述,我需要在 axios 调用函数之后刷新页面,但不是在收到承诺之前。函数触发后页面当前正在刷新,但未收到承诺。我想做的是向 api 提交一篇博文,当我在最后添加 preventDefault 函数时,一切正常。

这里功能:

handleSubmit(event){
    axios.post("https://saulvegablog.devcamp.space/portfolio/portfolio_blogs", this.buildForm(), {withCredentials:true})
    .then(response => {
      this.props.handleSuccessfullFormSubmission(response.data);
    }).catch(error => {
        console.log("handlesubmit error for blog ", error)
    })
    event.preventDefault();
}

【问题讨论】:

  • 那么,这里有问题吗?您是否在问为什么添加 event.preventDefault() 会修复您的表单在提交时重新加载页面? “刷新页面”是指反应的状态/道具在某处更新并反应重新呈现 UI,还是实际页面重新加载?

标签: reactjs axios


【解决方案1】:

你可以在then回调函数中重新加载你的页面,添加

window.location.reload()

喜欢这个

handleSubmit(event){
    event.preventDefault();
    axios.post("https://saulvegablog.devcamp.space/portfolio/portfolio_blogs", this.buildForm(), {withCredentials:true})
    .then(response => {
      this.props.handleSuccessfullFormSubmission(response.data);
      window.location.reload();
    }).catch(error => {
        console.log("handlesubmit error for blog ", error)
    })
}

【讨论】:

  • 我希望所有页面都重新加载,现在只有 vuejs 页面被重新加载。
猜你喜欢
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 2013-12-15
相关资源
最近更新 更多