el-switch切换时最好用input事件

本文介绍了一个Vue项目的实现细节,通过el-switch组件和@input事件,确保表格内的开关状态能实时与后端同步。在状态改变时,调用handleSwitch方法更新数据,并通过async/await处理异步操作,保证页面状态与后台数据一致。
摘要由CSDN通过智能技术生成

场景:开关按钮保持跟后端同步
在这里插入图片描述

#html
<el-table-column label="状态" align="center" prop="state" show-overflow-tooltip>
      <template slot-scope="scope">
        <el-switch :value="scope.row.state" @input="handleSwitch(scope.row)">
        </el-switch>
     </template>
</el-table-column>

#js
// 状态改变事件
async handleSwitch(row) {
      let params = {
        state: row.state ? '1' : '0',
        teamId: row.templetId,
        optType: 'del'
      }
      const res = await optTempletAndGradeWeight(params)
      console.log('状态改变', res);
      this.getList()
},

页面最好用value属性,不要用v-model,事件用@input事件,不要用@change事件,这样就可以保持同步啦

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值