-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zhongjiayao_v3_hooks): 新增 useOnline Hook 以检查浏览器在线状态
返回浏览器的在线状态
- Loading branch information
Null
committed
Aug 14, 2024
1 parent
a574a8a
commit 37af4bc
Showing
10 changed files
with
70 additions
and
3 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,8 @@ | ||
<template> | ||
<div>状态:{{ online ? '在线' : '离线' }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { useOnline } from 'zhongjiayao_v3_hooks'; | ||
const { online } = useOnline(); | ||
</script> |
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,4 @@ | ||
# useOnline | ||
|
||
返回浏览器的在线状态。参考 [Navigator:onLine 属性](https://developer.mozilla.org/zh-CN/docs/Web/API/Navigator/onLine) | ||
<preview path="./demo/index.vue" title="基本使用" description='useOnline'></preview> |
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,7 @@ | ||
# zhongjiayao_v3_hooks | ||
|
||
## 1.1.0 | ||
|
||
### Minor Changes | ||
|
||
- 新增 useOnline Hook 以检查浏览器在线状态 |
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
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,8 @@ | ||
<template> | ||
<div>状态:{{ online ? '在线' : '离线' }}</div> | ||
</template> | ||
|
||
<script setup> | ||
import { useOnline } from 'zhongjiayao_v3_hooks'; | ||
const { online } = useOnline(); | ||
</script> |
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,4 @@ | ||
# useOnline | ||
|
||
返回浏览器的在线状态。参考 [Navigator:onLine 属性](https://developer.mozilla.org/zh-CN/docs/Web/API/Navigator/onLine) | ||
<preview path="./demo/index.vue" title="基本使用" description='useOnline'></preview> |
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,31 @@ | ||
import { ref, onMounted, onUnmounted } from 'vue'; | ||
import useEventListener from '../useEventListener'; | ||
|
||
/** | ||
* @description 用户网络是否可用 | ||
* */ | ||
function useOnline() { | ||
const online = ref(true); | ||
const showStatus = (val) => { | ||
online.value = typeof val == 'boolean' ? val : val.target.online; | ||
}; | ||
|
||
// 在页面加载后,设置正确的网络状态 | ||
navigator.onLine ? showStatus(true) : showStatus(false); | ||
|
||
onMounted(() => { | ||
// 开始监听网络状态的变化 | ||
window.addEventListener('online', showStatus); | ||
|
||
window.addEventListener('offline', showStatus); | ||
}); | ||
onUnmounted(() => { | ||
// 移除监听网络状态的变化 | ||
window.removeEventListener('online', showStatus); | ||
|
||
window.removeEventListener('offline', showStatus); | ||
}); | ||
|
||
return { online }; | ||
} | ||
export default useOnline; |
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,3 @@ | ||
{ | ||
"type": "State" | ||
} |