Files
certd/packages/ui/certd-client/src/use/use-mounted.ts
T

25 lines
655 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { onActivated, onMounted } from "vue";
/**
* 可靠的页面刷新钩子:
* - 如果组件实际被 KeepAlive 缓存命中,由 onActivated 触发 init
* - 如果没有被缓存,由 onMounted 兜底触发,避免不刷新也不触发 onActivated。
*/
export function useMounted(init: () => void | Promise<void>) {
let activated = false;
onActivated(() => {
activated = true;
init();
});
onMounted(() => {
// 让 onActivated 有机会先执行;组件未被 KeepAlive 缓存时 onActivated 不会触发,由这里兜底。
setTimeout(() => {
if (!activated) {
init();
}
});
});
}