VUE控制按钮状态

1.  按钮标签上添加:disabled="btnState"

<el-button :disabled="btnState" type="primary" @click="submitFaFn">确认发货</el-button>

 2. 在data中声明变量

btnState:false

3.在js中根据业务进行判断

//已发货页面
      if (scope.id == "3") {
        this.titleCheck = "待收货";   // 标题
        this.btnState = false;        //按钮状态(false可以点击,true禁用)
      } else if (scope.id == "5") {
        this.titleCheck = "点击申请退款按钮";
        this.btnState = true;   //判断确认发布按钮状态
      } else if (scope.id == "4") {
        // this.centerDialogVisible = false; //已收货 关闭弹框(判断弹框)
        this.titleCheck = "已收货";
        this.btnState = true;
        
      } else if (scope.id == "2" || "1") {
        this.titleCheck = "待发货";
        this.btnState = false;
      }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Vue的数据绑定和计算属性来实现此功能。以下是示例代码: ``` <template> <div> <el-button :loading="button1Loading" @click="handleButton1Click" :disabled="button2Disabled">按钮1</el-button> <el-button :disabled="button1Loading" @click="handleButton2Click">按钮2</el-button> </div> </template> <script> export default { data() { return { button1Loading: false, button2Disabled: false, timer: null } }, computed: { isDisabled() { return this.button1Loading || this.button2Disabled } }, methods: { handleButton1Click() { this.button1Loading = true this.button2Disabled = true // 发送请求,等待响应 axios.get('/api/example') .then(response => { if (response.data.code === 2000) { // code == 2000,按钮状态不变 alert('请求成功') } else { // code != 2000,按钮状态恢复 this.resetButtons() } }) .catch(error => { console.log(error) // 请求错误,按钮状态恢复 this.resetButtons() }) // 设置定时器,10秒钟后恢复按钮状态 this.timer = setTimeout(() => { this.resetButtons() }, 10000) }, handleButton2Click() { alert('按钮2被点击') }, resetButtons() { this.button1Loading = false this.button2Disabled = false if (this.timer !== null) { clearTimeout(this.timer) } } } } </script> ``` 在`data`选项中,定义了`button1Loading`和`button2Disabled`两个状态变量用于控制两个按钮状态。在`computed`选项中,定义了一个计算属性`isDisabled`,计算出两个按钮是否处于禁用状态。在`methods`选项中,定义了三个方法:`handleButton1Click`处理按钮1的点击事件,`handleButton2Click`处理按钮2的点击事件,`resetButtons`恢复两个按钮的默认状态。在`handleButton1Click`方法中,首先设置两个按钮状态,然后发送请求,等待响应。(这里使用了axios库进行请求的发送与响应操作)如果响应数据的`code`值为`2000`,则说明请求成功,仅弹出提示框,并不改变按钮状态。否则,按钮状态恢复默认状态,超时时限10秒,会清除掉之前设置的超时,以避免多余的变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值