elementUI MessageBox prompt模式下异步提交问题

Cejauh 发布于 2019-08-31 3005 次阅读


AI 摘要

在使用elementUI的MessageBox时,你是否遇到过在prompt模式下异步提交数据碰壁的困扰?点击确定后,弹窗关闭,却再也没有后续指令的执行,让项目进展一度停滞。别担心!经过反复研究,本文将教你如何用callback方法轻松解决这一难题,确保在弹窗关闭后仍能顺利执行代码。让我们一起探索解决方案,解锁流畅的用户体验吧!

问题

最近写项目发现,使用elementUI的MessageBox的prompt 异步提交数据时,存在点了确定弹窗关闭并且不再执行then下面的指令,自然也就没办法继续执行。

经过一番测试,解决办法为:使用callback方法来代替Promise,示例代码如下

this.$prompt('请输入密码', '管理员登录', {
        confirmButtonText: '确定',
        inputPlaceholder: '密码必须包含大小写字母和数字的组合',
        cancelButtonText: '取消',
        inputPattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,16}$/,
        inputErrorMessage: '密码格式不正确',
        callback: function (action, instance) {
          if (action === 'confirm') {
            self.$axios({
              url: self.$serverUrl + '/index/Server/RconLogin',
              method: 'POST',
              data: {你要提交的数据}
            }).then(function (res) {
              if (res.data.status === 'success') {
                self.$message({
                  type: 'success',
                  message: '成功进入管理员模式!'
                })
                self.ServerInfoDialog.adminMode = true
              }
            }).catch(function (error) {
              self.$message({
                type: 'danger',
                message: error.response.data
              })
            })
          }
        }
      })

这样就可以在窗口关闭之后继续执行回调内的代码了。请注意 instance.inputValue 的值就是prompt内输入的信息

May we meet again.
最后更新于 2025-05-07