Skip to content

Commit

Permalink
fix: fix that the compatible of onLoad with useWatcher hook
Browse files Browse the repository at this point in the history
  • Loading branch information
JOU-amjs committed Jan 15, 2024
1 parent 82a17b3 commit 7287112
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/statesHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { onLoad } from '@dcloudio/uni-app';
import { EffectRequestParams } from 'alova';
import VueHook from 'alova/vue';
import { WatchSource } from 'vue';
import { len } from './helper';

export default {
...VueHook,
effectRequest(effectRequestParams: EffectRequestParams<WatchSource>) {
const handler = effectRequestParams.handler;
effectRequestParams.handler = () => {
// 延迟10ms让页面中的onLoad先执行
onLoad(() => {
setTimeout(handler, 10);
});
effectRequestParams.handler = (...args: any[]) => {
// 当没有参数时,表示为立即发送请求,此时延迟10ms让页面中的onLoad先执行
// 当有参数时,表示通过useWatcher状态改变时发送请求,此时直接调用handler即可
len(args) > 0
? handler(...args)
: onLoad(() => {
setTimeout(() => handler(...args), 10);
});
};
return VueHook.effectRequest(effectRequestParams);
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"skipDefaultLibCheck": false,
"inlineSourceMap": false,
"importHelpers": true,
"removeComments": true,
"removeComments": false,
"esModuleInterop": true,
"allowJs": true,
"types": ["@dcloudio/types", "jest", "node"]
Expand Down

0 comments on commit 7287112

Please sign in to comment.