【问题标题】:vuejs - show alert and redirect after submiting axios formvuejs - 提交 axios 表单后显示警报和重定向
【发布时间】:2019-08-21 02:53:46
【问题描述】:

我有一个工作正常并插入数据的表单,但是如果用户按下提交按钮 10 次,它会插入数据 10 次,所以我想将用户重定向到另一个页面并向他显示成功消息或错误。在这里我使用 axios,如下所示,我添加了 then 响应并捕获错误,但它没有显示我想我必须在 html 中显示一些方法:

reserve() {
            // this.form.post('../api/comment')
            axios.post('../api/reserve',{
                property_id:1,
                reserve_start:this.date,
                reserve_end:this.date2,
            }).then(response => {
                self.message = 'Data is entered'
            })
                .catch(error => {
                    self.message = 'Error'
                })

这是我的表单标记:

 <form @submit.prevent="reserve()"  id="myForm"  class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed">

【问题讨论】:

  • 另一种可能的解决方案是:一旦您的请求成功或拒绝,然后在 .then.catch 部分中重置表单输入。因此,这样做可以确保您不会立即通过。

标签: javascript vue.js axios


【解决方案1】:

在 axios 调用之后在方法中重定向非常简单。

类似这样的:

reserve() {
// this.form.post('../api/comment')
  axios.post('../api/reserve',{
    property_id:1,
    reserve_start:this.date,
    reserve_end:this.date2,
  }).then(response => {
    this.$router.push('/routeToGoTo')      
  })
}
.catch(error => {
  self.message = 'Error'
})

您可以在 router.push 之前发出警报,这将停止一切,直到它被关闭。所以警报会显示,用户点击确定,页面重定向。

alert("Something was done.")

【讨论】:

  • 我不知道为什么,但这不起作用它只是返回到同一页面而没有消息但仍然插入
  • @Farshad,你在this.$router.push('/routeToGoTo') 中放的是什么路由器?
猜你喜欢
  • 1970-01-01
  • 2014-04-13
  • 2014-06-25
  • 2010-10-05
  • 2022-11-19
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多