You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
598 B

  1. import http from 'libs/http'
  2. import { ref } from 'vue'
  3. export default function useApiData(url: string, params?: any, init?: boolean) {
  4. const data = ref(null)
  5. const loading = ref(false)
  6. const error = ref()
  7. const fetchData = async () => {
  8. loading.value = true
  9. error.value = null
  10. try {
  11. const response = await http.post(url, params)
  12. data.value = response.data
  13. }
  14. catch (err) {
  15. error.value = err
  16. }
  17. finally {
  18. loading.value = false
  19. }
  20. }
  21. init && fetchData().then(() => {})
  22. return {
  23. data,
  24. loading,
  25. error,
  26. fetchData,
  27. }
  28. }