subweb/src/components/loading/index.js

29 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 全局loading效果合并多次loading请求避免重复请求
* 当调一次showLoading则次数+1当次数为0时则显示loading
* 当调一次hideLoading则次数-1; 当次数为0时则结束loading
*/
import { ElLoading } from 'element-plus';
// 定义一个请求次数的变量,用来记录当前页面总共请求的次数
let loadingRequestCount = 0;
// 初始化loading
let loadingInstance;
// 编写一个显示loading的函数并且记录请求次数 ++
const showLoading = (target) => {
if (loadingRequestCount === 0) {
// element的服务方式 target 我这边取的是表格class
// 类似整个表格loading和在表格配置v-loading一样的效果这么做是全局实现了不用每个页面单独去v-loading
loadingInstance = ElLoading.service({ target });
}
loadingRequestCount++;
};
// 编写一个隐藏loading的函数并且记录请求次数 --
const hideLoading = () => {
if (loadingRequestCount <= 0) return;
loadingRequestCount--;
if (loadingRequestCount === 0) {
loadingInstance.close();
}
};
export { showLoading, hideLoading };