Skip to content

Commit

Permalink
feat(hooks): add vue2.7 setup hooks fun.
Browse files Browse the repository at this point in the history
  • Loading branch information
WuChenDi committed Jul 26, 2024
1 parent c868555 commit 348a315
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 05-Vue/vue-components/hooks/vue2-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { getCurrentInstance, ref, computed } from 'vue'
import { Loading } from 'element-ui'

// import {
// useRoute,
// useRouter,
// useLink,
// onBeforeRouteUpdate,
// onBeforeRouteLeave,
// } from 'vue-router/composables'

/**
* @deprecated
*
* 使用替代 `import { useRoute } from 'vue-router/composables'`
*/
export const useRoute = () => {
const instance = getCurrentInstance()
if (!instance) throw new Error('useRoute must be called in setup')

return instance.proxy.$route
}

/**
* @deprecated
*
* 使用替代 `import { useRouter } from 'vue-router/composables'`
*/
export const useRouter = () => {
const instance = getCurrentInstance()
if (!instance) throw new Error('useRouter must be called in setup')

return instance.proxy.$router
}

export const useStore = () => {
const instance = getCurrentInstance()
if (!instance) throw new Error('useStore must be called in setup')

return instance.proxy.$store
}

export const useStoreGetters = () => {
const store = useStore()
return Object.fromEntries(
Object.keys(store.getters).map((getter) => [
getter,
computed(() => store.getters[getter]),
])
)
}

export const mapGetter = (key) => {
const store = useStore()
return computed(() => store.getters[key])
}

export const useLoading = (target) => {
const loading = ref()
const openLoading = () => {
loading.value = Loading.service({ target, fullscreen: false })
}
const closeLoading = () => {
loading.value && loading.value.close()
}

return [openLoading, closeLoading]
}

0 comments on commit 348a315

Please sign in to comment.