From 77c9c3fd2558c1758ae433fa22909658a581a73d Mon Sep 17 00:00:00 2001 From: liuyun Date: Sun, 11 Oct 2026 16:46:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20useRequest=20=E8=AF=B7=E6=B1=82=E5=BA=8F?= =?UTF-8?q?=E5=8F=B7=E9=98=B2=E7=AB=9E=E6=80=81=20+=20ty-crud=20=E6=90=9C?= =?UTF-8?q?=E7=B4=A2/=E9=87=8D=E7=BD=AE=E5=8E=BB=E9=87=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- src/components/ty-crud.vue | 15 +++++++++++---- src/composables/useRequest.ts | 6 +++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/ty-crud.vue b/src/components/ty-crud.vue index f6e0384..ac9ef93 100644 --- a/src/components/ty-crud.vue +++ b/src/components/ty-crud.vue @@ -173,13 +173,20 @@ watch([page, pageSize], () => { }) function handleSearch(): void { - page.value = 1 - fetchList() + resetAndFetch() } function handleReset(): void { - page.value = 1 - fetchList() + resetAndFetch() +} + +/** 重置到第一页并查询:page 已是 1 时 watch 不触发,需手动查询,避免重复请求。 */ +function resetAndFetch(): void { + if (page.value === 1) { + fetchList() + } else { + page.value = 1 + } } async function openCreate(): Promise { diff --git a/src/composables/useRequest.ts b/src/composables/useRequest.ts index 2239eef..c0144a1 100644 --- a/src/composables/useRequest.ts +++ b/src/composables/useRequest.ts @@ -47,10 +47,12 @@ export function useRequest( let lastParams: TParams = defaultParams let controller: AbortController | null = null + let seq = 0 async function run(...args: TParams): Promise { controller?.abort() controller = new AbortController() + const mySeq = ++seq lastParams = args.length > 0 ? args : lastParams loading.value = true @@ -59,16 +61,18 @@ export function useRequest( try { const result = await fetcher(...lastParams, controller.signal) + if (mySeq !== seq) return undefined // 已被更新的请求取代 data.value = result options.onSuccess?.(result, lastParams) return result } catch (e) { if (isCanceled(e)) return undefined + if (mySeq !== seq) return undefined error.value = e options.onError?.(e, lastParams) return undefined } finally { - loading.value = false + if (mySeq === seq) loading.value = false options.onFinally?.(lastParams) } }