diff --git a/src/hook/index.ts b/src/hook/index.ts index 2bb5a6f..8a6054a 100644 --- a/src/hook/index.ts +++ b/src/hook/index.ts @@ -1 +1,3 @@ export * from './useAppRouter'; +export * from './useBoolean'; +export * from './useLoading'; diff --git a/src/hook/useBoolean.ts b/src/hook/useBoolean.ts new file mode 100644 index 0000000..5be3a36 --- /dev/null +++ b/src/hook/useBoolean.ts @@ -0,0 +1,30 @@ +import { ref } from 'vue'; + +/** + * boolean组合式函数 + * @param initValue 初始值 + */ +export function useBoolean(initValue = false) { + const bool = ref(initValue); + + function setBool(value: boolean) { + bool.value = value; + } + function setTrue() { + setBool(true); + } + function setFalse() { + setBool(false); + } + function toggle() { + setBool(!bool.value); + } + + return { + bool, + setBool, + setTrue, + setFalse, + toggle, + }; +} diff --git a/src/hook/useLoading.ts b/src/hook/useLoading.ts new file mode 100644 index 0000000..d3e80d8 --- /dev/null +++ b/src/hook/useLoading.ts @@ -0,0 +1,11 @@ +import { useBoolean } from './useBoolean'; + +export function useLoading(initValue = false) { + const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(); + + return { + loading, + startLoading, + endLoading, + }; +} diff --git a/src/views/list/commonList/index.vue b/src/views/list/commonList/index.vue index 4e8ef10..eb7bb3a 100644 --- a/src/views/list/commonList/index.vue +++ b/src/views/list/commonList/index.vue @@ -4,25 +4,25 @@ - 条件1 + 姓名 - 条件2 + 年龄 - 条件3 + 性别 - 条件4 + 地址 @@ -56,8 +56,8 @@ 下载 - - + + @@ -68,7 +68,9 @@ import { onMounted, ref, h } from 'vue'; import { fetchUserList } from '~/src/service/api/mock'; import type { DataTableColumns } from 'naive-ui'; import { NButton, NPopconfirm, NSpace, NSwitch, NTag } from 'naive-ui'; +import { useLoading } from '@/hook'; +const { loading, startLoading, endLoading } = useLoading(false); const model = ref({ condition_1: '', condition_2: '', @@ -164,10 +166,15 @@ interface UserList { const listData = ref([]); onMounted(() => { - fetchUserList().then((res) => { - listData.value = res.data; - }); + getUserList(); }); +async function getUserList() { + startLoading(); + await fetchUserList().then((res) => { + listData.value = res.data; + endLoading(); + }); +} function changePage(page: number, size: number) { window.$message.success(`分页器:${page},${size}`); }