use-async.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { isReactive, isRef, onMounted } from 'vue';
  2. import { RouterView, useRoute, useRouter } from 'vue-router';
  3. import { Searchs } from '@/utils/searchs';
  4. function setLoading(loading: any, val: any) {
  5. if (loading != undefined && isRef(loading)) {
  6. loading.value = val;
  7. } else if (loading != undefined && isReactive(loading)) {
  8. loading.loading = val;
  9. }
  10. }
  11. export const useAsync = async (
  12. func: Promise<any>,
  13. loading: any
  14. ): Promise<any> => {
  15. setLoading(loading, true);
  16. return await func.finally(() => setLoading(loading, false));
  17. };
  18. export const getTabsCache = (callBack: any) => {
  19. const route = useRoute();
  20. const searchs = new Searchs(route.path);
  21. const active = searchs.get(route.path);
  22. onMounted(() => {
  23. callBack(active);
  24. });
  25. };
  26. export const setTabsCaches = (current: any, key = 'current', routes: any) => {
  27. const searchs = new Searchs(routes.path);
  28. searchs.update({ [key]: current }, undefined, 'form');
  29. const active = searchs.get(routes.path);
  30. console.log(active, 'setTabsCaches');
  31. };