提交 | 用户 | 时间
|
314507
|
1 |
import { ref, onBeforeUnmount } from 'vue' |
H |
2 |
|
|
3 |
const useNetwork = () => { |
|
4 |
const online = ref(true) |
|
5 |
|
|
6 |
const updateNetwork = () => { |
|
7 |
online.value = navigator.onLine |
|
8 |
} |
|
9 |
|
|
10 |
window.addEventListener('online', updateNetwork) |
|
11 |
window.addEventListener('offline', updateNetwork) |
|
12 |
|
|
13 |
onBeforeUnmount(() => { |
|
14 |
window.removeEventListener('online', updateNetwork) |
|
15 |
window.removeEventListener('offline', updateNetwork) |
|
16 |
}) |
|
17 |
|
|
18 |
return { online } |
|
19 |
} |
|
20 |
|
|
21 |
export { useNetwork } |