diff --git a/src/hooks/useRequest.ts b/src/hooks/useRequest.ts index 80ff33d..017a710 100644 --- a/src/hooks/useRequest.ts +++ b/src/hooks/useRequest.ts @@ -1,4 +1,4 @@ -import type { UnwrapRef } from 'vue' +import type { Ref } from 'vue' interface IUseRequestOptions { /** 是否立即执行 */ @@ -7,6 +7,13 @@ interface IUseRequestOptions { initialData?: T } +interface IUseRequestReturn { + loading: Ref + error: Ref + data: Ref + run: () => Promise +} + /** * useRequest是一个定制化的请求钩子,用于处理异步请求和响应。 * @param func 一个执行异步请求的函数,返回一个包含响应数据的Promise。 @@ -18,15 +25,15 @@ interface IUseRequestOptions { export default function useRequest( func: () => Promise>, options: IUseRequestOptions = { immediate: false }, -) { +): IUseRequestReturn { const loading = ref(false) const error = ref(false) - const data = ref(options.initialData) + const data = ref(options.initialData) as Ref const run = async () => { loading.value = true return func() .then((res) => { - data.value = res.data as UnwrapRef + data.value = res.data error.value = false return data.value })