当前位置:   article > 正文

vue3+element-plus 如何实现全局配置ElLoading默认值_ele vue3 loading 设置全局

ele vue3 loading 设置全局

项目场景:

公司项目中使用element-plus的loading效果时想更改默认的loading图标


问题描述

遗憾的是el-config-provider没有对应的全局配置属性,无法通过全局配置简单实现。


解决方案:

第一时间想到用覆盖样式的方式解决,但思考之后觉得不是很完美,于是研究了第二套方案,废话不多说,直接上代码。

方案一:

大致就是将图中svg标签隐藏掉,给el-loading-spinner类添加before或after,再结合自定义的样式和动画实现loading效果。

方案二:

通过重写ElLoading的方式实现,具体如下:

1、在plugins文件夹下新建wrapElLoading.ts文件

wrapElLoading.ts

  1. import {ElLoading} from "element-plus";
  2. /**
  3. * @description 扩展ElLoading,传入默认值
  4. */
  5. export default {
  6. install(app: any) {
  7. const svg: string = `<path d="M21.38,4.58a4.62,4.62,0,0,0,9.24,0h0a4.62,4.62,0,0,0-9.24,0Z" fill="#9ab1c9"/><path d="M6.33,10.39a3.75,3.75,0,0,0,7.49,0h0a3.75,3.75,0,0,0-7.49,0Z" fill="#9ab1c9"/><path d="M0,25.86a3.43,3.43,0,0,0,6.86,0h0a3.41,3.41,0,0,0-3.43-3.4A3.41,3.41,0,0,0,0,25.85Z" fill="#9ab1c9"/><path d="M6.88,41.66a3.2,3.2,0,1,0,3.2-3.17A3.18,3.18,0,0,0,6.88,41.66Z" fill="#9ab1c9"/><path d="M23.32,47.35a2.68,2.68,0,0,0,5.36,0h0a2.68,2.68,0,0,0-5.36,0Z" fill="#9ab1c9"/><path d="M39.68,41.66a2.14,2.14,0,0,0,4.28,0h0a2.14,2.14,0,0,0-4.28,0Z" fill="#9ab1c9"/><path d="M46.85,26A1.58,1.58,0,0,0,50,26h0a1.58,1.58,0,0,0-3.15,0Z" fill="#9ab1c9"/><path d="M40.77,10.2a1.12,1.12,0,1,0,1.12-1.11A1.12,1.12,0,0,0,40.77,10.2Z" fill="#9ab1c9"/>`;
  8. const loadingDir: any = ElLoading.directive
  9. const originDirMounted = loadingDir.mounted;
  10. loadingDir.mounted = function (el: any, binding: any, vnode: any, prevVnode: any) {
  11. // 需要覆盖哪些默认属性值在这里设置,具体属性名参考官网loading指令用法
  12. el.setAttribute('element-loading-svg', svg)
  13. originDirMounted.call(this, el, binding, vnode, prevVnode)
  14. }
  15. const originService = ElLoading.service;
  16. ElLoading.service = function (options: any = {}) {
  17. return originService.call(this, Object.assign({ svg}, options))
  18. }
  19. app.config.globalProperties.$loading = ElLoading.service;
  20. // 如果在main.ts中全局使用了ElementPlus —> app.use(ElementPlus),则下面这行代码不需要
  21. // app.use(ElLoading);
  22. }
  23. }

2、在main.ts中use即可

  1. import WrapElLoading from "@/plugins/wrapElLoading";
  2. // 若项目采用全局引入ElementPlus,则须在app.use(ElementPlus)后执行
  3. app.use(WrapElLoading);

3、在组件中使用:

  1. <template>
  2. <div class="about">
  3. <el-button @click="showLoading">show loading</el-button>
  4. <el-button @click="manualLoading">manual loading</el-button>
  5. <div v-loading="loading" element-loading-text="加载中……" class="loading-wrap"></div>
  6. <div ref="manualLoadingRef" class="loading-wrap"></div>
  7. </div>
  8. </template>
  9. <script setup lang="ts">
  10. import {getCurrentInstance, ref} from "vue";
  11. const ctx: any = getCurrentInstance()?.appContext.config.globalProperties
  12. const loading = ref(false);
  13. const manualLoadingRef = ref();
  14. function showLoading() {
  15. loading.value = !loading.value
  16. }
  17. function manualLoading() {
  18. const loadingIns = ctx.$loading({target: manualLoadingRef.value})
  19. setTimeout(() => {
  20. loadingIns.close()
  21. }, 2000)
  22. }
  23. </script>
  24. <style lang="scss" scoped>
  25. .about {
  26. border: 1px solid #42b983;
  27. height: 100%;
  28. padding: 60px;
  29. .loading-wrap {
  30. width: 200px;
  31. height: 200px;
  32. border: 1px solid #ccc;
  33. }
  34. }
  35. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/119088
推荐阅读
相关标签
  

闽ICP备14008679号