2022-10-06 09:15:30 +00:00
|
|
|
---
|
|
|
|
description: This wrapper around useFetch triggers navigation immediately.
|
|
|
|
---
|
|
|
|
|
2022-04-06 05:56:08 +00:00
|
|
|
# `useLazyFetch`
|
|
|
|
|
2022-10-06 09:15:30 +00:00
|
|
|
`useLazyFetch` provides a wrapper around `useFetch` that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
|
|
|
2022-04-14 10:45:53 +00:00
|
|
|
## Description
|
2022-04-14 08:53:58 +00:00
|
|
|
|
2022-04-14 10:45:53 +00:00
|
|
|
By default, [useFetch](/api/composables/use-fetch) blocks navigation until its async handler is resolved.
|
2022-04-06 05:56:08 +00:00
|
|
|
|
2022-04-14 10:45:53 +00:00
|
|
|
> `useLazyFetch` has the same signature as `useFetch`.
|
|
|
|
|
|
|
|
:ReadMore{link="/api/composables/use-fetch"}
|
2022-04-06 05:56:08 +00:00
|
|
|
|
2022-04-14 08:53:58 +00:00
|
|
|
## Example
|
|
|
|
|
|
|
|
```vue
|
|
|
|
<template>
|
|
|
|
<div v-if="pending">
|
|
|
|
Loading ...
|
|
|
|
</div>
|
|
|
|
<div v-else>
|
|
|
|
<div v-for="post in posts">
|
|
|
|
<!-- do something -->
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2022-04-14 10:45:53 +00:00
|
|
|
/* Navigation will occur before fetching is complete.
|
2022-04-16 13:53:36 +00:00
|
|
|
Handle pending and error states directly within your component's template
|
2022-04-14 10:45:53 +00:00
|
|
|
*/
|
2022-04-14 08:53:58 +00:00
|
|
|
const { pending, data: posts } = useLazyFetch('/api/posts')
|
|
|
|
watch(posts, (newPosts) => {
|
|
|
|
// Because posts starts out null, you won't have access
|
|
|
|
// to its contents immediately, but you can watch it.
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
2022-09-13 12:54:31 +00:00
|
|
|
:ReadMore{link="/getting-started/data-fetching#uselazyfetch"}
|