Skip to content

Commit

Permalink
perf: useNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Sep 27, 2023
1 parent 1db2248 commit 88be3ee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mock/role/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ const adminList = [
meta: {
title: 'useClipboard'
}
},
{
path: 'useNetwork',
component: 'views/hooks/useNetwork',
name: 'UseNetwork',
meta: {
title: 'useNetwork'
}
}
]
},
Expand Down Expand Up @@ -635,6 +643,7 @@ const testList: string[] = [
'/hooks/useValidator',
'/hooks/useCrudSchemas',
'/hooks/useClipboard',
'/hooks/useNetwork',
'/level',
'/level/menu1',
'/level/menu1/menu1-1',
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/web/useNetwork.ts
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 }
8 changes: 8 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
meta: {
title: 'useClipboard'
}
},
{
path: 'useNetwork',
component: () => import('@/views/hooks/useNetwork.vue'),
name: 'UseNetwork',
meta: {
title: 'useNetwork'
}
}
]
},
Expand Down
12 changes: 12 additions & 0 deletions src/views/hooks/useNetwork.vue
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>

0 comments on commit 88be3ee

Please sign in to comment.