vue3 封装loading.js

vue3 封装loading.js

utils/loading.js

/**
 * 全局loading效果:合并多次loading请求,避免重复请求
 */
import { ElLoading } from "element-plus";
let loadingRequestCount = 0;
let loadingInstance;
// 显⽰loading的函数并且记录请求次数 ++
const showLoading = () => {
  if (loadingRequestCount === 0) {
    loadingInstance = ElLoading.service({
      lock: true,
      text: "加载中……",
      background: 'rgba(0, 0, 0, 0.1)'
    });
  }
  loadingRequestCount++;
};
// 隐藏loading的函数,并且记录请求次数 --
const hideLoading = () => {
  if (loadingRequestCount <= 0) return;
  loadingRequestCount--;
  if (loadingRequestCount === 0) {
    loadingInstance.close();
  }
};
export { showLoading, hideLoading };

你可能感兴趣的:(Web前端,javascript,前端,开发语言)