feat: add useNetwork hooks

pull/278/MERGE
xingyu 2023-10-31 10:11:34 +08:00
parent 4ba264e00d
commit f8fdebffe6
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import { ref, onBeforeUnmount } from 'vue'
const useNetwork = () => {
const online = ref(true)
const updateNetwork = () => {
online.value = navigator.onLine
}
window.addEventListener('online', updateNetwork)
window.addEventListener('offline', updateNetwork)
onBeforeUnmount(() => {
window.removeEventListener('online', updateNetwork)
window.removeEventListener('offline', updateNetwork)
})
return { online }
}
export { useNetwork }