当前位置: 首页 > news >正文

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

先看效果:

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

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

开关组件源码:

<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>

父组件:

<template><custom-switchv-model="switchValue":loading="switchLoading":active-value="1":inactive-value="0":disabled="switchDisabled"@switch-click="switchClick"/>
</template>
<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 : 0this.switchLoading = false}, 2000)}}
}
</script>

http://www.lryc.cn/news/163658.html

相关文章:

  • 【GAN小白入门】Semi-Supervised GAN 理论与实战
  • Python自动化测试(1)-自动化测试及基本技术手段概述
  • 小程序中如何查看会员的余额和变更记录
  • 【项目经验】elementui--table表格自定义表头及bug
  • flink实现kafka、doris精准一次说明
  • 【git】git commit、push之前自动执行脚本
  • 基于springboot+vue的加盟店管理系统(前后端分离)
  • Gin中的Cookie和Session的用法
  • 【算法】反悔贪心
  • Hadoop的安装和使用,Windows使用shell命令简单操作HDFS
  • ubuntu上ffmpeg使用framebuffer显示video
  • 82 # koa-bodyparser 中间件的使用以及实现
  • 计算一串输出数字的累加和
  • python包导入原理解析
  • MNIST手写数字辨识-cnn网路 (机器学习中的hello world,加油)
  • 论文笔记《3D Gaussian Splatting for Real-Time Radiance Field Rendering》
  • 数据库管理系统,数据库,sql的基本介绍以及它们之间的关系
  • 【Flowable】Springboot使用Flowable(一)
  • 戳泡泡小游戏
  • Redis缓存
  • mysql 插入更新数据
  • 系统架构设计高级技能 · 软件产品线
  • C语言学习系列-->字符函数和字符串函数
  • 尖端AR技术如何在美国革新外科手术实践?
  • 【木板】Python实现-附ChatGPT解析
  • 第一章:绪论
  • C++面试知识点总结
  • 从智能手机到智能机器人:小米品牌的高端化之路
  • 深度学习推荐系统(八)AFM模型及其在Criteo数据集上的应用
  • 【Spring】aop的底层原理