refactor: 重构 useFullscreenLoading 代码细节
This commit is contained in:
parent
f121c01227
commit
592fa82650
@ -1,15 +1,6 @@
|
|||||||
import type { LoadingOptions } from "element-plus"
|
import type { LoadingOptions } from "element-plus"
|
||||||
import { ElLoading } from "element-plus"
|
import { ElLoading } from "element-plus"
|
||||||
|
|
||||||
const defaultOptions = {
|
|
||||||
lock: true,
|
|
||||||
text: "加载中..."
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LoadingInstance {
|
|
||||||
close: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UseFullscreenLoading {
|
interface UseFullscreenLoading {
|
||||||
<T extends (...args: Parameters<T>) => ReturnType<T>>(
|
<T extends (...args: Parameters<T>) => ReturnType<T>>(
|
||||||
fn: T,
|
fn: T,
|
||||||
@ -17,8 +8,17 @@ interface UseFullscreenLoading {
|
|||||||
): (...args: Parameters<T>) => Promise<ReturnType<T>>
|
): (...args: Parameters<T>) => Promise<ReturnType<T>>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface LoadingInstance {
|
||||||
|
close: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_OPTIONS = {
|
||||||
|
lock: true,
|
||||||
|
text: "加载中..."
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 传入一个函数 fn,在它执行周期内,加上「全屏」loading
|
* 传入一个函数 fn,在它执行周期内,加上「全屏」Loading
|
||||||
* @param fn 要执行的函数
|
* @param fn 要执行的函数
|
||||||
* @param options LoadingOptions
|
* @param options LoadingOptions
|
||||||
* @returns 返回一个新的函数,该函数返回一个 Promise
|
* @returns 返回一个新的函数,该函数返回一个 Promise
|
||||||
@ -27,10 +27,10 @@ export const useFullscreenLoading: UseFullscreenLoading = (fn, options = {}) =>
|
|||||||
let loadingInstance: LoadingInstance
|
let loadingInstance: LoadingInstance
|
||||||
return async (...args) => {
|
return async (...args) => {
|
||||||
try {
|
try {
|
||||||
loadingInstance = ElLoading.service({ ...defaultOptions, ...options })
|
loadingInstance = ElLoading.service({ ...DEFAULT_OPTIONS, ...options })
|
||||||
return await fn(...args)
|
return await fn(...args)
|
||||||
} finally {
|
} finally {
|
||||||
loadingInstance?.close()
|
loadingInstance.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user