码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • element-ui switch开关组件二次封装,添加loading效果,点击时调用接口后改变状态


    先看效果:

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

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

    开关组件源码:

    1. <template>
    2. <div class="custom-switch" @click="switchClick">
    3. <div style="width: fit-content;height: fit-content;" v-loading="loading">
    4. <el-switch style="position: relative;" v-bind="$attrs">el-switch>
    5. div>
    6. div>
    7. template>
    8. <script>
    9. /**
    10. * el-switch开关组件二次封装
    11. *
    12. * description:
    13. * 移除了el-switch的change事件
    14. * 添加了loading效果
    15. * 开关的value值交给父组件处理
    16. */
    17. export default {
    18. name: 'CustomSwitch',
    19. props: {
    20. loading: {
    21. default: false,
    22. type: Boolean
    23. }
    24. },
    25. data() {
    26. return {}
    27. },
    28. created() {},
    29. mounted() {},
    30. methods: {
    31. switchClick() {
    32. // 如果禁用和loading状态,不emit给父组件
    33. if (this.$attrs.disabled || this.loading) {
    34. return
    35. }
    36. this.$emit('switch-click', this.$attrs.value)
    37. }
    38. }
    39. }
    40. script>
    41. <style lang="scss" scoped>
    42. .custom-switch {
    43. width: 100%;
    44. height: 100%;
    45. display: flex;
    46. align-items: center;
    47. justify-content: center;
    48. ::v-deep .el-loading-mask {
    49. width: 100%;
    50. height: 100%;
    51. border-radius: 10px;
    52. top: 2px;
    53. .el-loading-spinner {
    54. position: relative;
    55. width: 100%;
    56. height: 100%;
    57. top: unset;
    58. margin-top: unset;
    59. display: flex;
    60. align-items: center;
    61. justify-content: center;
    62. svg {
    63. width: 20px;
    64. height: 20px;
    65. }
    66. }
    67. }
    68. }
    69. style>

    父组件:

    1. <template>
    2. <custom-switch
    3. v-model="switchValue"
    4. :loading="switchLoading"
    5. :active-value="1"
    6. :inactive-value="0"
    7. :disabled="switchDisabled"
    8. @switch-click="switchClick"
    9. />
    10. template>
    1. <script>
    2. import CustomSwitch from './custom-switch.vue'
    3. export default {
    4. components: { CustomSwitch },
    5. data() {
    6. return {
    7. switchValue: 1,
    8. switchLoading: false,
    9. switchDisabled: false
    10. }
    11. },
    12. methods: {
    13. switchClick() {
    14. this.switchLoading = true
    15. // 这里就可以调用接口,接口成功后修改值和loading状态
    16. setTimeout(() => {
    17. this.switchValue = !this.switchValue ? 1 : 0
    18. this.switchLoading = false
    19. }, 2000)
    20. }
    21. }
    22. }
    23. script>

  • 相关阅读:
    Odoo:全球排名第一的免费开源PLM管理系统介绍
    内点法(interior point method)求解二次规划,附python代码
    微擎模块 万能门店小程序7.4.1带九大插件 【增加】DIY商品模块,优化拼团产品页部分样式摇一摇插件
    OpenCV入门9:图像增强和图像滤波
    Mechanize
    Go语言进阶 --- Time包的使用
    【100天精通Python】Day57:Python 数据分析_Pandas数据描述性统计,分组聚合,数据透视表和相关性分析
    网络安全(黑客技术)—自学
    linux学习笔记
    ReentrantLock 先删再批量保存 ReentrantLock有啥用
  • 原文地址:https://blog.csdn.net/u011295864/article/details/132766860
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号