2022-10-06 09:15:30 +00:00
---
2023-10-18 10:59:43 +00:00
title: 'useLazyFetch'
2022-10-06 09:15:30 +00:00
description: This wrapper around useFetch triggers navigation immediately.
2023-10-18 10:59:43 +00:00
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/fetch.ts
size: xs
2022-10-06 09:15:30 +00:00
---
2022-04-14 10:45:53 +00:00
## Description
2022-04-14 08:53:58 +00:00
2023-10-18 10:59:43 +00:00
By default, [`useFetch` ](/docs/api/composables/use-fetch ) blocks navigation until its async handler is resolved. `useLazyFetch` provides a wrapper around [`useFetch` ](/docs/api/composables/use-fetch ) that triggers navigation before the handler is resolved by setting the `lazy` option to `true` .
2022-04-06 05:56:08 +00:00
2024-02-21 17:09:45 +00:00
::note
2023-10-18 10:59:43 +00:00
`useLazyFetch` has the same signature as [`useFetch` ](/docs/api/composables/use-fetch ).
::
2022-04-14 10:45:53 +00:00
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/api/composables/use-fetch"}
2022-04-06 05:56:08 +00:00
2022-04-14 08:53:58 +00:00
## Example
2023-10-18 10:59:43 +00:00
```vue [pages/index.vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
/* Navigation will occur before fetching is complete.
Handle pending and error states directly within your component's template
*/
const { pending, data: posts } = await useLazyFetch('/api/posts')
watch(posts, (newPosts) => {
// Because posts might start out null, you won't have access
// to its contents immediately, but you can watch it.
})
< / script >
2022-04-14 08:53:58 +00:00
< template >
< div v-if = "pending" >
Loading ...
< / div >
< div v-else >
< div v-for = "post in posts" >
<!-- do something -->
< / div >
< / div >
< / template >
```
2024-02-21 17:09:45 +00:00
::note
2023-02-03 11:55:58 +00:00
`useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch` .
::
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/getting-started/data-fetching"}