useRefresh.ts 762 B

12345678910111213141516171819202122232425
  1. import { onUnmounted } from "vue";
  2. // 回调池
  3. const cbPool: (() => void)[] = [];
  4. // 注册顶部按钮刷新回调(组件销毁时,自动销毁回调, 这是推荐的方式)
  5. export function registerRefreshCallback(cb: () => void) {
  6. cbPool.push(cb);
  7. onUnmounted(() => {
  8. const targetIndex = cbPool.findIndex((item) => item === cb);
  9. if (targetIndex !== -1) {
  10. cbPool.splice(targetIndex, 1);
  11. }
  12. console.log("cbPool", cbPool.length);
  13. });
  14. }
  15. // 注册顶部按钮刷新回调(组件销毁时,不会自动销毁回调,慎用)
  16. export function registerRefreshCallbackForever(cb: () => void) {
  17. cbPool.push(cb);
  18. }
  19. // 调用触发刷新事件回调
  20. export function invokeAllRefreshCallback() {
  21. cbPool.forEach((cb) => cb && cb());
  22. }