Appearance
useRequest
- Last Changed: 2023-09-06
请求状态管理器。
代码演示
基础用法
全局配置
// main.ts
import { setGlobalOptions } from '@gauss/vueuse'
// 设置全局配置
setGlobalOptions({
manual: true,
//...
})// main.ts
import { setGlobalOptions } from '@gauss/vueuse'
// 设置全局配置
setGlobalOptions({
manual: true,
//...
})API
Params
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| service | 返回 Promise 的函数 | (...params) => Promise | |
| config | 配置对象 | Object |
config 具体参数
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| manual | 手动请求 | boolean | Ref<boolean> | true |
| retryCount | 错误重试次数 | number | Ref<number> | 0 |
| retryInterval | 错误重试时间间隔,单位毫秒 | number | Ref<number> | 0 |
| debounceInterval | 防抖,单位毫秒 | number | Ref<number> | 0 |
| throttleInterval | 节流,单位毫秒 | number | Ref<number> | 0 |
| ready | 依赖请求 | boolean | Ref<boolean> | true |
| defaultParams | 默认请求参数 | any[] | Ref<any[]> | [] |
| onError | 错误回调 | (data: any) => void | |
| onSuccess | 成功回调 | (err: any) => void |
返回
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| data | 请求成功返回数据 | Ref<any> | |
| error | 请求失败返回数据 | Ref<any> | |
| loading | 请求中状态 | Ref<any> | |
| params | 请求参数 | Ref<any[]> | [] |
| run | 手动执行方法 | (...params) => void | |
| runAsync | run 用法一致,返回 promise,retryCount 不生效,不触发 onError,onSuccess 回调 | (...params) => void | |
| cancel | promise 执行中断 | () => void |
Gauss