-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): add vue2.7 setup hooks fun.
- Loading branch information
Showing
1 changed file
with
68 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
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] | ||
} |