Releases: AttoJS/vue-request
v2.0.4
v2.0.3
v2.0.2
v2.0.1
变更列表
-
使用
vue-demi兼容 vue2 #38 -
新增自定义缓存
getCache、setCache和clearCache。 -
开启缓存的情况下,设置了相同
cacheKey的请求将会被缓存和复用。 -
新增
runAsync和refreshAsync,将返回Promise。 -
新增
definePlugin,可以通过插件来扩展 useRequest 的功能。 -
节流/防抖模式下可以使用
runAsync返回正常的Promise。 -
新增
useRequestProviderhooks,用于注入 options 配置。 -
新增
refreshDepsAction选项,用于自定义refreshDeps触发后的行为。 -
refreshDepsAction在manual=true时,也会被refreshDeps的改变而触发。 -
新增
loadingKeep。 -
移除 内部集成请求库,
service不再支持字符或对象。 迁移帮助 -
移除
formatResult。 迁移帮助 -
移除
queryKey,即移除了并行模式 迁移帮助 -
run不再返回Promise迁移帮助 -
请求出错时,
data不再会被清空#82 -
修改
ready的逻辑 迁移帮助 -
ready支持传入一个返回布尔值的函数 #166 -
data和error改为shallowRef -
usePagination移除了reload方法和reloading。如需要对应的需求,可自行实现。 -
移除了
RequestConfig组件 迁移帮助 -
重构了
useLoadMore,具体 API 可查看详情 API 说明 -
cacheKey支持传入函数:cacheKey: (params?: P) => string -
refreshDeps支持传入 一个函数,返回一个值 、是一个 ref 、一个响应式对象 或是由以上类型的值组成的数组 #166useRequest(getUser,{ cacheKey: (params?:P):string => { <!-- 初始化时,params 会为 undefined,需要手动判断并返回一个空字符串 --> if(params){ return `user-key-${params[0].name}` } return '' } })
-
部分
options支持响应式,如下所示type ReactivityOptions = { loadingDelay: number | Ref<number>; loadingKeep: number | Ref<number>; pollingInterval: number | Ref<number>; debounceInterval: number | Ref<number>; debounceOptions: DebounceOptions | Reactive<DebounceOptions>; throttleInterval: number | Ref<number>; throttleOptions: ThrottleOptions | Reactive<ThrottleOptions>; refreshOnWindowFocus: boolean | Ref<boolean>; refocusTimespan: number | Ref<number>; errorRetryCount: number | Ref<number>; errorRetryInterval: number | Ref<number>; };
迁移帮助
const getUser = userName => {
return axios.get("api/user", {
params: {
name: userName,
},
});
};
useRequest(getUser, options);const getUser = async () => {
const results = await axios.get("api/user");
// 在此处处理最终的数据
return results.data;
};-
- 当
manual=false时,每次ready从false变为true时,都会自动发起请求,会带上参数options.defaultParams。 - 当
manual=true时,只要ready为false,则无法发起请求。
- 当
useLoadMore API
Options
| 参数 | 说明 | 类型 |
|---|---|---|
| manual | 当设置为 true 时,你需要手动触发 loadMore 或者 loadMoreAsync 才会发起请求。默认为 false |
boolean |
| ready | 当 manual=false 时,每次 ready 从 false 变为 true 时,都会自动触发 refresh。当 manual=true 时,只要 ready 为 false,则无法发起请求。 |
Ref<boolean> | () => boolean |
| refreshDeps | 改变后自动触发 refresh,如果设置了 refreshDepsAction 则触发 refreshDepsAction |
WatchSource<any> | WatchSource<any>[] |
| refreshDepsAction | refreshDeps改变后触发 |
() => void |
| debounceInterval | 以防抖策略处理请求 | number | Ref<number> |
| debounceOptions | 防抖参数 | {leading: false, maxWait: undefined, trailing: true} |
| throttleInterval | 以节流策略处理请求 | number | Ref<number> |
| throttleOptions | 节流参数 | {leading: false, trailing: true} |
| errorRetryCount | 发生错误时的错误重试次数 | number | Ref<number> |
| errorRetryInterval | 发生错误时的错误重试间隔时间 | number | Ref<number> |
| isNoMore | 判断是否还有更多数据 | (data?: R) => boolean |
| onBefore | service 执行前触发 | () => void |
| onAfter | service 执行完成时触发 | () => void |
| onSuccess | service resolve 时触发 | (data: R) => void |
| onError | service reject 时触发 | (error: Error) => void |
Result
| 参数 | 说明 | 类型 |
|---|---|---|
| data | service 返回的数据,必须包含 list 数组,类型为 { list: any[], ...other } |
Ref<R> |
| dataList | data 的 list 数组 |
Ref<R['list']> |
| loading | 是否正在进行请求 | Ref<boolean> |
| loadingMore | 是否正在加载更多 | Ref<boolean> |
| noMore | 是否有更多数据,需要配合 options.isNoMore 使用 |
Ref<boolean> |
| error | service 返回的错误 | Error |
| loadMore | 加载更多数据,会自动捕获异常,通过 options.onError 处理 |
() => void |
| loadMoreAsync | 加载更多数据,返回 Promise,需要自行处理错误 |
() => Promise<R> |
| refresh | 刷新加载第一页数据,会自动捕获异常,通过 options.onError 处理 |
() => void |
| refreshAsync | 刷新加载第一页数据,返回 Promise,需要自行处理错误 |
() => Promise<R> |
| mutate | 直接修改 data 的结果 |
(arg: (oldData: R) => R) => void | (newData: R) => void |
| cancel | 取消请求 | () => void |
Changelog
- Use
vue-demito be compatible with Vue 2 #38 - Added custom cache
getCache,setCache, andclearCache. - When caching is enabled, requests with the same
cacheKeywill be cached and reused. - Added
runAsyncandrefreshAsync, which return aPromise. - Added
definePluginto extend the functionality of useRequest through plugins. - In throttle/debounce mode,
runAsynccan be used to return a normalPromise. - Added
useRequestProviderhooks to inject options configuration. - Added
refreshDepsActionoption to customize the behavior afterrefreshDepsis triggered. refreshDepsActionwill also be triggered by changes inrefreshDepswhenmanual=true.- Added
loadingKeep. - Removed the integrated request library and
serviceno longer supports strings or objects. Migration Guide - Removed
formatResult. Migration Guide - Removed
queryKey, i.e., parallel mode is removed Migration Guide runno longer returns aPromiseMigration Guide- When a request fails,
datais no longer cleared #82 - Modified the logic of
readyMigration Guide - **
readynow supports passing a function t...