Просмотр исходного кода

refactor(hooks): 使 useRequest 的参数 P 可选并设置默认类型

修改 IUseRequestReturn 和 useRequest 的泛型参数 P 为可选,并设置默认类型为 undefined
使 run 方法的参数变为可选,提高 hook 的灵活性
feige996 7 месяцев назад
Родитель
Сommit
9fb68fa807
1 измененных файлов с 7 добавлено и 6 удалено
  1. 7 6
      src/hooks/useRequest.ts

+ 7 - 6
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<T> {
 interface IUseRequestOptions<T> {
   /** 是否立即执行 */
   /** 是否立即执行 */
@@ -7,11 +8,11 @@ interface IUseRequestOptions<T> {
   initialData?: T
   initialData?: T
 }
 }
 
 
-interface IUseRequestReturn<T, P> {
+interface IUseRequestReturn<T, P = undefined> {
   loading: Ref<boolean>
   loading: Ref<boolean>
   error: Ref<boolean | Error>
   error: Ref<boolean | Error>
   data: Ref<T | undefined>
   data: Ref<T | undefined>
-  run: (args: P) => Promise<T | undefined>
+  run: (args?: P) => Promise<T | undefined>
 }
 }
 
 
 /**
 /**
@@ -22,14 +23,14 @@ interface IUseRequestReturn<T, P> {
  * @param options.initialData 初始化数据,默认为undefined。
  * @param options.initialData 初始化数据,默认为undefined。
  * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。
  * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。
  */
  */
-export default function useRequest<T, P>(
-  func: (args: P) => Promise<T>,
+export default function useRequest<T, P = undefined>(
+  func: (args?: P) => Promise<T>,
   options: IUseRequestOptions<T> = { immediate: false },
   options: IUseRequestOptions<T> = { immediate: false },
 ): IUseRequestReturn<T, P> {
 ): IUseRequestReturn<T, P> {
   const loading = ref(false)
   const loading = ref(false)
   const error = ref(false)
   const error = ref(false)
   const data = ref<T | undefined>(options.initialData) as Ref<T | undefined>
   const data = ref<T | undefined>(options.initialData) as Ref<T | undefined>
-  const run = async (args: P) => {
+  const run = async (args?: P) => {
     loading.value = true
     loading.value = true
     return func(args)
     return func(args)
       .then((res) => {
       .then((res) => {