zhitong.yu
8 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ref, onMounted, onUnmounted } from "vue";
 
/**
 * @description 网络是否可用
 * */
export const useOnline = () => {
  const online = ref(true);
  const showStatus = (val: any) => {
    online.value = typeof val == "boolean" ? val : val.target.online;
  };
  // 在页面加载后,设置正确的网络状态
  navigator.onLine ? showStatus(true) : showStatus(false);
 
  onMounted(() => {
    // 开始监听网络状态的变化
    window.addEventListener("online", showStatus);
    window.addEventListener("offline", showStatus);
  });
 
  onUnmounted(() => {
    // 移除监听网络状态的变化
    window.removeEventListener("online", showStatus);
    window.removeEventListener("offline", showStatus);
  });
 
  return { online };
};