useLoading.js 560 B

12345678910111213141516171819202122232425
  1. export const useLoading = () => {
  2. const isLoading = ref(false)
  3. const loadingMessage = ref('')
  4. const setLoading = (loading, message = '처리 중...') => {
  5. isLoading.value = loading
  6. loadingMessage.value = message
  7. }
  8. const withLoading = async (asyncFn, message = '처리 중...') => {
  9. try {
  10. setLoading(true, message)
  11. return await asyncFn()
  12. } finally {
  13. setLoading(false)
  14. }
  15. }
  16. return {
  17. isLoading: readonly(isLoading),
  18. loadingMessage: readonly(loadingMessage),
  19. setLoading,
  20. withLoading
  21. }
  22. }