-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1db2248
commit 88be3ee
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="ts"> | ||
import { ContentWrap } from '@/components/ContentWrap' | ||
import { useNetwork } from '@/hooks/web/useNetwork' | ||
const { online } = useNetwork() | ||
</script> | ||
|
||
<template> | ||
<ContentWrap title="useNetwork"> | ||
当前网络状态: <code>{{ online ? '已连接' : '已断开' }}</code> | ||
</ContentWrap> | ||
</template> |