useLazyAsyncData

Description

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

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

useLazyAsyncData has the same signature as useAsyncData.

👉

Read more in API > Composables > Use Async Data.

Example

  1. <template>
  2. <div>
  3. {{ pending ? 'Loading' : count }}
  4. </div>
  5. </template>
  6. <script setup>
  7. /* Navigation will occur before fetching is complete.
  8. Handle pending and error states directly within your component's template
  9. */
  10. const { pending, data: count } = useLazyAsyncData('count', () => $fetch('/api/count'))
  11. watch(count, (newCount) => {
  12. // Because count starts out null, you won't have access
  13. // to its contents immediately, but you can watch it.
  14. })
  15. </script>

👉

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