码农知识堂 - 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>

  • 相关阅读:
    HTML5期末大作业【红色的电影售票平台网站】web前端 html+css+javascript网页设计实例 企业网站制作
    影视解说短视频如何配音?三个文字转语音小技巧,配音其实也不难
    分享一个你很可能不知道的Java异常实现的缺陷
    基于51单片机的简易可调时钟闹钟Proteus仿真
    软件测试知识库+1,5款顶级自动化测试工具推荐和使用分析
    跟我学企业级flutter项目:简化框架demo参考
    Python实践项目——LSB隐写术
    x86架构基础汇编知识
    解析gin框架部分收获
    [LeetCode]剑指 Offer 40. 最小的k个数
  • 原文地址:https://blog.csdn.net/u011295864/article/details/132766860
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号