| 12345678910111213141516171819202122232425 |
- import { onUnmounted } from "vue";
- // 回调池
- const cbPool: (() => void)[] = [];
- // 注册顶部按钮刷新回调(组件销毁时,自动销毁回调, 这是推荐的方式)
- export function registerRefreshCallback(cb: () => void) {
- cbPool.push(cb);
- onUnmounted(() => {
- const targetIndex = cbPool.findIndex((item) => item === cb);
- if (targetIndex !== -1) {
- cbPool.splice(targetIndex, 1);
- }
- console.log("cbPool", cbPool.length);
- });
- }
- // 注册顶部按钮刷新回调(组件销毁时,不会自动销毁回调,慎用)
- export function registerRefreshCallbackForever(cb: () => void) {
- cbPool.push(cb);
- }
- // 调用触发刷新事件回调
- export function invokeAllRefreshCallback() {
- cbPool.forEach((cb) => cb && cb());
- }
|