【Element】el-switch开关 点击弹窗确认框时状态先改变----点击弹窗取消框失效

一、背景

需求:在列表中添加定期出账的开关按钮,点击开关时,原来的状态不改变,弹出弹窗;点击弹窗取消按钮:状态不改变,点击弹窗确定按钮:状态改变,并调取列表数据刷新页面

二、具体实现

使用element el-switch开关,具体用法可查看官方指引

官网指引:Element - The world's most popular Vue UI framework

javascript 复制代码
<template slot-scope="scope">
 <el-switch
  @change="openSwitch($event, scope.row)"
  v-model="scope.row.regularlyBill"
  active-color="#6CD354"
  :active-value="'1'"
  :inactive-value="'0'"
  >
 </el-switch>
</template>
javascript 复制代码
methods: {

    /**更改开关状态 val=1是打开时  val=0是关闭时 */
    openSwitch(val, row) {
      if(val == 0){
        this.$Remind({
          title:'关闭之后,自动出账将失效,但仍可进行手工操作出账,确定要关闭吗?'
        }).then(()=>{
          this.isLoading = true;
          this.changeResultBill(row) //调取接口更改开关状态
        })
      }else{
        this.$Remind({
          title:'确定要开启自动定期出账吗?'
        }).then(()=>{
          this.isLoading = true;
          this.changeResultBill(row) //调取接口更改开关状态
        })
      }
    },
    //修改开关状态
    changeResultBill(row){
      let params = {
          id:row.id,
          regularlyBill:row.regularlyBill
        }
      this.$http.post(this.$url.lifebill.updateRegularlyBill,params).then(res=>{
        this.isLoading = false;
        if(res.code == 0){
          this.$message.success(res.msg)
          this.getDateTemplatePage(); //调取列表接口刷新页面
        }
      })
    },
}

备注:

①this.$Remind是自定义封装的弹窗,弹窗组件在element上也有,具体选择按需求为主

②v-model:是数据绑定值,实现数据双向绑定;active-color:switch 打开时的背景色;active-value:switch 打开时的值;inactive-value:switch 关闭时的值

三、效果展示

四、踩坑记录

4.1、问题描述

**问题1:**点击打开或关闭按钮,弹窗还未点击确定,开关的状态已经先改变了

**问题2:**点击弹窗取消按钮,开关状态也是变化后的状态,开关状态应不改变

4.2、原因分析并解决

原因:v-model 实现数据双向绑定,点击开关时状态就实时发生变化

解决:将v-model改成:value="",再赋值即可。调取更改开关状态的接口时也传递状态值

4.3、更改后的代码

javascript 复制代码
<template slot-scope="scope">
<!-- 更改前 v-model="scope.row.regularlyBill" -->
 <el-switch
  @change="openSwitch($event, scope.row)"
  v-model="scope.row.regularlyBill"
  active-color="#6CD354"
  :active-value="'1'"
  :inactive-value="'0'"
  >
 </el-switch>
</template>
javascript 复制代码
methods: {

    /**更改开关状态 val=1是打开时  val=0是关闭时 */
    openSwitch(val, row) {
      if(val == 0){
        this.$Remind({
          title:'关闭之后,自动出账将失效,但仍可进行手工操作出账,确定要关闭吗?'
        }).then(()=>{
          this.isLoading = true;
          //this.changeResultBill(row) //更改前--调取接口更改开关状态
           this.changeResultBill(val,row) //更改后--调取接口更改开关状态,并传入开关状态的值
        }).catch(()=>{
          this.$message.error("取消了关闭操作"); //增加了取消按钮的弹窗提示
        })
      }else{
        this.$Remind({
          title:'确定要开启自动定期出账吗?'
        }).then(()=>{
          this.isLoading = true;
           //this.changeResultBill(row) //更改前--调取接口更改开关状态
           this.changeResultBill(val,row) //更改后--调取接口更改开关状态,并传入开关状态的值
        }).catch(()=>{
          this.$message.error("取消了开启操作"); //增加了取消按钮的弹窗提示
        })
      }
    },
    //修改开关状态
    changeResultBill(val,row){
      let params = {
          id:row.id,
          //regularlyBill:row.regularlyBill //更改前
          regularlyBill:val //更改后
        }
      this.$http.post(this.$url.lifebill.updateRegularlyBill,params).then(res=>{
        this.isLoading = false;
        if(res.code == 0){
          this.$message.success(res.msg)
          this.getDateTemplatePage(); //调取列表接口刷新页面
        }
      })
    },
}

4.4、bug修复后的效果

最终:

点击开关状态不变,弹出弹窗;点击弹窗取消按钮:开关状态不变,并给与取消提示;点击弹窗确定按钮:状态改变,并调取列表数据刷新页面
**最后:**👏👏 😀😀😀 👍👍

相关推荐
阿蒙Amon4 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1274 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian5 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo5 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk5 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程5 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525546 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin1233227 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos
李白你好7 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
经年未远8 小时前
vue3中实现耳机和扬声器切换方案
javascript·学习·vue