web: 添加hooks

This commit is contained in:
Xwite
2023-05-13 09:42:28 +08:00
parent 15178d1313
commit 31e10b3010
4 changed files with 121 additions and 114 deletions

View File

@@ -0,0 +1,36 @@
import { watch, unref, onUnmounted } from "vue";
import { ElLoading } from "element-plus";
import loadingSvg from "@element-plus/icons-svg/loading.svg?raw";
import "element-plus/theme-chalk/el-loading.css";
import "./loading.css";
export const useLoading = (target, text, spinner = loadingSvg) => {
// loading spinner
const isLoading = ref(false);
let loadingInstance = null;
const closeLoading = () => (isLoading.value = false);
const showLoading = () => (isLoading.value = true);
watch(isLoading, (loading) => {
if (!loading) return loadingInstance?.close();
loadingInstance = ElLoading.service({
target: unref(target),
spinner: spinner,
text: text,
lock: true,
background: "rgba(0, 0, 0, 0)",
});
});
const loadingWrapper = (promise) => {
if (!(promise instanceof Promise))
throw TypeError("loadingWrapper argument must be Promise");
showLoading();
return promise.finally(closeLoading);
};
onUnmounted(() => {
closeLoading();
});
return { isLoading, showLoading, closeLoading, loadingWrapper };
};