-
-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug Report] isBreak 拦截请求后状态未被正确更新 #226
Labels
enhancement
New feature or request
Comments
在 #225 被 merge 之前,搭配 useBeforePlugin,可以通过这样的操作来完成参数校验与请求拦截: const { data, loading, mutate } = useRequest(, {
onBefore(params) {
// 参数校验
if(true) {
nextTick(() => {
loading.value = false
// mutate( ... )
})
return false
}
}
}, [useBeforePlugin]) useBeforePlugin 具体实现// useBeforePlugin.ts
import { definePlugin } from 'vue-request';
export interface BeforeResult {
/**
* 是否阻止请求
*/
isBreak?: boolean;
/**
* 阻止请求时,返回的数据
*/
breakResult?: any;
}
/**
* useBeforePlugin
* 为 onBefore 钩子函数补充拦截请求的能力
* onBefore 返回 false 时,会阻止请求
* onBefore 返回对象时,会将对象整个返回给 useRequest
* @param {Function} onBefore - 请求前的钩子函数
*/
export const useBeforePlugin = definePlugin((_, { onBefore }) => {
return {
onBefore(params) {
// @ts-expect-error
const res: boolean | BeforeResult = onBefore?.(params);
console.log('onBefore', res);
if (res === false) {
return {
isBreak: true,
};
}
if (typeof res === 'object' && res !== null) {
return res;
}
return {};
},
};
}); |
John60676
added a commit
that referenced
this issue
Jan 10, 2024
Add `isReturn` to handle situations where the request needs to be aborted and custom data returned.
自定义插件中的 |
breakResult 直接被干掉了? |
这次更新本身就属于 breaking change,所以调整是无法避免的。而且自定义插件使用的人不多 |
什么时候可以发版解决这个问题呢?👀 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug 描述 Bug description
在插件中使用 onBefore 钩子对前置参数进行校验,并选择性拦截请求
拦截后,状态没有被正确重置
代码重现 Reproduce
我编写了一个插件 useBeforePlugin 来辅助完成 onBefore 生命周期中的参数校验与请求拦截功能
但是它并没有按预期工作:
Demo 链接
期望结果 Desired result
当插件在 onBefore 时返回 isBreak 为 true 时:
其他信息 Other information
#225
#204
The text was updated successfully, but these errors were encountered: