更新
This commit is contained in:
@@ -8,6 +8,7 @@ interface Options {
|
||||
params?: Record<any, any>
|
||||
fixedParams?: Record<any, any>
|
||||
firstLoading?: boolean
|
||||
latestOnly?: boolean
|
||||
}
|
||||
|
||||
export function usePaging(options: Options) {
|
||||
@@ -17,7 +18,8 @@ export function usePaging(options: Options) {
|
||||
fetchFun,
|
||||
params = {},
|
||||
fixedParams = {},
|
||||
firstLoading = false
|
||||
firstLoading = false,
|
||||
latestOnly = false
|
||||
} = options
|
||||
// 记录分页初始参数
|
||||
const paramsInit: Record<any, any> = Object.assign({}, toRaw(params))
|
||||
@@ -30,8 +32,10 @@ export function usePaging(options: Options) {
|
||||
lists: [] as any[],
|
||||
extend: {} as Record<string, any>
|
||||
})
|
||||
let latestRequestId = 0
|
||||
// 请求分页接口;silent: true 时不改 loading(用于定时静默刷新,避免表格闪 loading)
|
||||
const getLists = (opts?: { silent?: boolean }) => {
|
||||
const requestId = ++latestRequestId
|
||||
const silent = opts?.silent === true
|
||||
if (!silent) {
|
||||
pager.loading = true
|
||||
@@ -43,16 +47,26 @@ export function usePaging(options: Options) {
|
||||
...fixedParams
|
||||
})
|
||||
.then((res: any) => {
|
||||
if (latestOnly && requestId !== latestRequestId) {
|
||||
return Promise.resolve(res)
|
||||
}
|
||||
pager.count = res?.count
|
||||
pager.lists = res?.lists
|
||||
pager.extend = res?.extend
|
||||
return Promise.resolve(res)
|
||||
})
|
||||
.catch((err: any) => {
|
||||
if (latestOnly && requestId !== latestRequestId) {
|
||||
return Promise.resolve(undefined)
|
||||
}
|
||||
return Promise.reject(err)
|
||||
})
|
||||
.finally(() => {
|
||||
if (!silent) {
|
||||
if (latestOnly) {
|
||||
if (requestId === latestRequestId) {
|
||||
pager.loading = false
|
||||
}
|
||||
} else if (!silent) {
|
||||
pager.loading = false
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user