element-ui switch开关组件二次封装,添加loading效果,点击时调用接口后改变状态

先看效果:

element-ui中的switch开关无loading属性(在element-plus时加入了),而且点击时开关状态就会切换,这使得在需要调用接口后再改变开关状态变得比较麻烦。

思路:switch开关外包一层div,给div添加click事件,emit给父组件,在父组件里进行开关状态的切换。

开关组件源码:

html 复制代码
<template>
  <div class="custom-switch" @click="switchClick">
    <div style="width: fit-content;height: fit-content;" v-loading="loading">
      <el-switch style="position: relative;" v-bind="$attrs"></el-switch>
    </div>
  </div>
</template>

<script>
/**
 * el-switch开关组件二次封装
 * 
 * description:
 * 移除了el-switch的change事件
 * 添加了loading效果
 * 开关的value值交给父组件处理
 */
export default {
  name: 'CustomSwitch',
  props: {
    loading: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {}
  },
  created() {},
  mounted() {},
  methods: {
    switchClick() {
      // 如果禁用和loading状态,不emit给父组件
      if (this.$attrs.disabled || this.loading) {
        return
      }
      this.$emit('switch-click', this.$attrs.value)
    }
  }
}
</script>
<style lang="scss" scoped>
.custom-switch {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  ::v-deep .el-loading-mask {
    width: 100%;
    height: 100%;
    border-radius: 10px;
    top: 2px;
    .el-loading-spinner {
      position: relative;
      width: 100%;
      height: 100%;
      top: unset;
      margin-top: unset;
      display: flex;
      align-items: center;
      justify-content: center;
      svg {
        width: 20px;
        height: 20px;
      }
    }
  }
}
</style>

父组件:

html 复制代码
<template>
    <custom-switch
        v-model="switchValue"
        :loading="switchLoading"
        :active-value="1"
        :inactive-value="0"
        :disabled="switchDisabled"
        @switch-click="switchClick"
    />
</template>
html 复制代码
<script>
import CustomSwitch from './custom-switch.vue'

export default {
    components: { CustomSwitch },
    data() {
        return {
            switchValue: 1,
            switchLoading: false,
            switchDisabled: false
        }
    },
    methods: {
        switchClick() {
            this.switchLoading = true
            // 这里就可以调用接口,接口成功后修改值和loading状态
            setTimeout(() => {
                this.switchValue = !this.switchValue ? 1 : 0
                this.switchLoading = false
            }, 2000)
        }
    }
}
</script>
相关推荐
zhong liu bin7 分钟前
Vue框架技术详解——项目驱动概念理解【前端】【Vue】
前端·javascript·vue.js·vscode·vue
前端 贾公子26 分钟前
ElementUI 中 validateField 对部分表单字段数组进行校验时多次回调问题
前端·javascript·elementui
棒棒的唐26 分钟前
vue2 elementUI 登录页面实现回车提交登录的方法
前端·javascript·elementui
zhangzuying102630 分钟前
基于Vue3 +ElementuiPlus + Dexie.js自研的浏览器插件新建标签页tab
vue.js·typescript·echarts
lichong95135 分钟前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
知识分享小能手1 小时前
React学习教程,从入门到精通,React 使用属性(Props)创建组件语法知识点与案例详解(15)
前端·javascript·vue.js·学习·react.js·前端框架·vue
知识分享小能手7 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
魔云连洲7 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
前端工作日常10 小时前
我学习到的Vue2.6的prop修饰符
vue.js
小菜全10 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json