types: 优化 useFullscreenLoading 的类型(闭包的泛型)
This commit is contained in:
parent
e878399d9e
commit
42a9967952
@ -9,6 +9,12 @@ interface ILoadingInstance {
|
|||||||
close: () => void
|
close: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IUseFullscreenLoading {
|
||||||
|
<T extends (...args: any[]) => ReturnType<T>>(fn: T, options?: LoadingOptions): (
|
||||||
|
...args: Parameters<T>
|
||||||
|
) => Promise<ReturnType<T>> | ReturnType<T>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 传入一个函数 fn,在它执行周期内,加上「全屏」loading,
|
* 传入一个函数 fn,在它执行周期内,加上「全屏」loading,
|
||||||
* 如果:
|
* 如果:
|
||||||
@ -19,10 +25,7 @@ interface ILoadingInstance {
|
|||||||
* @param options LoadingOptions
|
* @param options LoadingOptions
|
||||||
* @returns Function 一个新的函数,去执行它吧
|
* @returns Function 一个新的函数,去执行它吧
|
||||||
*/
|
*/
|
||||||
export function useFullscreenLoading<T>(
|
export const useFullscreenLoading: IUseFullscreenLoading = (fn, options = {}) => {
|
||||||
fn: (...args: any[]) => T | Promise<T>,
|
|
||||||
options: LoadingOptions = {}
|
|
||||||
): (...args: any[]) => Promise<T> {
|
|
||||||
let loadingInstance: ILoadingInstance
|
let loadingInstance: ILoadingInstance
|
||||||
const showLoading = (options: LoadingOptions) => {
|
const showLoading = (options: LoadingOptions) => {
|
||||||
loadingInstance = ElLoading.service(options)
|
loadingInstance = ElLoading.service(options)
|
||||||
@ -31,7 +34,7 @@ export function useFullscreenLoading<T>(
|
|||||||
loadingInstance && loadingInstance.close()
|
loadingInstance && loadingInstance.close()
|
||||||
}
|
}
|
||||||
const _options = { ...defaultOptions, ...options }
|
const _options = { ...defaultOptions, ...options }
|
||||||
return (...args: any[]) => {
|
return (...args) => {
|
||||||
try {
|
try {
|
||||||
showLoading(_options)
|
showLoading(_options)
|
||||||
const result = fn(...args)
|
const result = fn(...args)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user