useLazyFetch

Description

By default, useFetch blocks navigation until its async handler is resolved.

useLazyFetch provides a wrapper around useFetch that triggers navigation before the handler is resolved by setting the lazy option to true.

useLazyFetch has the same signature as useFetch.

👉

Read more in API > Composables > Use Fetch.

Example

  1. <template>
  2. <div v-if="pending">
  3. Loading ...
  4. </div>
  5. <div v-else>
  6. <div v-for="post in posts">
  7. <!-- do something -->
  8. </div>
  9. </div>
  10. </template>
  11. <script setup>
  12. /* Navigation will occur before fetching is complete.
  13. Handle pending and error states directly within your component's template
  14. */
  15. const { pending, data: posts } = useLazyFetch('/api/posts')
  16. watch(posts, (newPosts) => {
  17. // Because posts starts out null, you won't have access
  18. // to its contents immediately, but you can watch it.
  19. })
  20. </script>

👉

Read more in Guide > Features > Data Fetching #uselazyfetch.