Skip to content

Instantly share code, notes, and snippets.

@DesignHhuang
Created September 28, 2023 10:46
Show Gist options
  • Save DesignHhuang/3e12daa017aadc63fc752538abd69d9a to your computer and use it in GitHub Desktop.
Save DesignHhuang/3e12daa017aadc63fc752538abd69d9a to your computer and use it in GitHub Desktop.
useNetworkStatus:使用网络状态
import { onMounted, onUnmounted } from 'vue';
export const useNetworkStatus = (callback = () => { }) => {
const updateOnlineStatus = () => {
const status = navigator.onLine ? 'online' : 'offline';
callback(status);
}
onMounted(() => {
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
});
onUnmounted(() => {
window.removeEventListener('online', updateOnlineStatus);
window.removeEventListener('offline', updateOnlineStatus);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment