diff --git a/src/hooks/useRequest.ts b/src/hooks/useRequest.ts index 731b56c..8ac4bfe 100644 --- a/src/hooks/useRequest.ts +++ b/src/hooks/useRequest.ts @@ -1,4 +1,5 @@ -import { type Ref, ref } from 'vue' +import type { Ref } from 'vue' +import { ref } from 'vue' interface IUseRequestOptions { /** 是否立即执行 */ @@ -7,11 +8,11 @@ interface IUseRequestOptions { initialData?: T } -interface IUseRequestReturn { +interface IUseRequestReturn { loading: Ref error: Ref data: Ref - run: (args: P) => Promise + run: (args?: P) => Promise } /** @@ -22,14 +23,14 @@ interface IUseRequestReturn { * @param options.initialData 初始化数据,默认为undefined。 * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。 */ -export default function useRequest( - func: (args: P) => Promise, +export default function useRequest( + func: (args?: P) => Promise, options: IUseRequestOptions = { immediate: false }, ): IUseRequestReturn { const loading = ref(false) const error = ref(false) const data = ref(options.initialData) as Ref - const run = async (args: P) => { + const run = async (args?: P) => { loading.value = true return func(args) .then((res) => {