Created
September 28, 2023 10:46
-
-
Save DesignHhuang/3e12daa017aadc63fc752538abd69d9a to your computer and use it in GitHub Desktop.
useNetworkStatus:使用网络状态
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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