Vue中实现避免按钮多次点击造成发送多次重复请求

store文件中设置一个变量,全局管理按钮的禁用和解除禁用,方便组件之间的通信
在这里插入图片描述
select.vue发送表单组件,表单没有填写完整时也要将按钮禁用解除

<Button type="primary" :disabled="this.$store.state.isDisable" @click="filterBtn('form')" icon="ios-search">筛选</Button>
methods: {
	filterBtn (name) {
       	  this.$store.state.isDisable = true
          this.$refs[name].validate((valid) => {
            if (valid) {
              this.$store.state.chartForm = this.requestData()         
              this.$Message.success(
                {
                  content:'发送请求成功!',
                  duration:3,
                }
              );
            } else {
              this.$Message.error('请求失败,请检查是否完整填写表单!');
              this.$store.state.isDisable = false  //解除按钮禁用
            }
          })
     }
 }

lineChart.vue获取数据显示曲线组件,不论获取到后端怎样的数据,都将按钮禁用解除,置为false

methods: {
        getData(){
          this.axios.post('/api/search/getgraph/predict/linechart', JSON.stringify(this.form),
              {headers:{'Content-Type':'application/json;charset=UTF-8'}}).then(result => {
            console.log("linePredictData:",result.data.data.coordinate)
           	........
            if (result.data.code === 200 && result.data.data.coordinate.length > 1) {
              .......
              this.flag = true
              this.$Message.success(
                {
                  content:'预测曲线更新完成',
                  duration: 8,
                  closable: true
                }
              );
              this.$store.state.isDisable = false   //解除按钮禁用
              this.myChart.clear()
              this.init()
            } else if(result.data.code === 200 && result.data.data.coordinate.length === 1){
              this.$Message.info({
                content: '此时间段内无数据,请重新选择',
                top:100,
                duration: 8,
                closable: true
              });
              this.$store.state.isDisable = false  //解除按钮禁用
              this.myChart.clear()
              this.init()
            }else if(result.data.data === null){
              this.flag = false
              this.$Message.error({
                content: '预测模块超时,请联系预测人员',
                duration: 8,
                closable: true
              });
              this.$store.state.isDisable = false  //解除按钮禁用
            }
          }).catch((error) => console.log(error))
        },
    }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值