前言:
在element中使用loadding的时候,在js中调用的,还是不太方便,这里把他封装下。
先写一个 loading.js 文件:
import { Loading } from 'element-ui';
let loadingCount = 0;
let loading;
const startLoading = () => {
loading = Loading.service({
lock: true,
text: '加载中……',
background: 'rgba(0, 0, 0, 0.7)'
});
};
const endLoading = () => {
loading.close();
};
export const showLoading = () => {
if (loadingCount === 0) {
startLoading();
}
loadingCount += 1;
};
export const hideLoading = () => {
if (loadingCount <= 0) {
return;
}
loadingCount -= 1;
if (loadingCount === 0) {
endLoading();
}
};
调用:
import { showLoading, hideLoading } from './loading';
showLoading();//打开加载中
hideLoading();//关闭加载中