Skip to content

Commit

Permalink
feat(zhongjiayao_v3_hooks): 新增 useOnline Hook 以检查浏览器在线状态
Browse files Browse the repository at this point in the history
返回浏览器的在线状态
  • Loading branch information
Null committed Aug 14, 2024
1 parent a574a8a commit 37af4bc
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/generate-router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Router = {
Dom: ['useClickAway', 'useEventListener', "useFullscreen", 'useHover', 'useRect', "useTitle", 'useWindowSize', "useDocumentVisibility", "useVirtualList", "useResizeObserver", "useElementBounding"],
State: ["useBoolean", "useDebounce", "useLocalStorage", "useThrottle", 'useToggle'],
State: ["useBoolean", "useDebounce", "useLocalStorage", "useThrottle", 'useToggle', "useOnline"],
Worker: ['useCountDown'],
Effect: ["useDebounceFn", "useThrottleFn"]
};
Expand Down
8 changes: 8 additions & 0 deletions docs/useOnline/demo/index.vue
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>
4 changes: 4 additions & 0 deletions docs/useOnline/index.md
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>
7 changes: 7 additions & 0 deletions packages/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# zhongjiayao_v3_hooks

## 1.1.0

### Minor Changes

- 新增 useOnline Hook 以检查浏览器在线状态
2 changes: 1 addition & 1 deletion packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhongjiayao_v3_hooks",
"version": "1.0.0",
"version": "1.1.0",
"description": "vue3 业务 Hooks",
"main": "./lib/index.js",
"module": "./es/index.js",
Expand Down
4 changes: 3 additions & 1 deletion packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import useDocumentVisibility from "./useDocumentVisibility";
import useVirtualList from "./useVirtualList";
import useResizeObserver from "./useResizeObserver";
import useElementBounding from "./useElementBounding";
import useOnline from "./useOnline";
export {
useBoolean,
useToggle,
Expand All @@ -36,5 +37,6 @@ export {
useDocumentVisibility,
useVirtualList,
useResizeObserver,
useElementBounding
useElementBounding,
useOnline
};
8 changes: 8 additions & 0 deletions packages/hooks/src/useOnline/demo/index.vue
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>
4 changes: 4 additions & 0 deletions packages/hooks/src/useOnline/index.md
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>
31 changes: 31 additions & 0 deletions packages/hooks/src/useOnline/index.ts
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;
3 changes: 3 additions & 0 deletions packages/hooks/src/useOnline/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "State"
}

0 comments on commit 37af4bc

Please sign in to comment.