防抖
通过设置 debounceWait 参数,useRequest 会对频繁的请求进行防抖处理,有效减少不必要的网络请求。
防抖的核心思想:等待用户完成操作后再执行,适用于搜索框、文本编辑器实时保存 等场景。
基础用法
ts
const { data, run } = useRequest(searchService, {
debounceWait: 1000,
});Vue3Request 的防抖是使用 lodash 提供的 debounce 实现的
你可以通过options.debounceOptions 来自定义 debounce 的行为。
Options
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| debounceWait | 防抖等待时间(毫秒) | number | Ref<number> | - |
| debounceOptions | leading: 指定调用在防抖开始前,trailing: 指定调用在防抖结束后 | { leading?: boolean, trailing?: boolean } | { leading: false, trailing: true } |
