/** 全局请求加载状态 — 由 axios 拦截器自动维护 */ import { ref, watch } from 'vue' /** 当前在途请求数 */ const pendingCount = ref(0) /** 是否显示加载条(延迟 300ms 后为 true,避免闪烁) */ const showBar = ref(false) let timer: ReturnType | null = null watch(pendingCount, (count) => { if (count > 0) { // 请求开始后 300ms 才显示(快请求不闪条) if (timer === null) { timer = setTimeout(() => { showBar.value = true }, 300) } } else { // 所有请求完成 → 立即隐藏 showBar.value = false if (timer !== null) { clearTimeout(timer); timer = null } } }) export function useGlobalLoading() { function increment() { pendingCount.value++ } function decrement() { if (pendingCount.value > 0) pendingCount.value-- } return { pendingCount, showBar, increment, decrement } }