mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-22 16:39:58 +00:00
docs: remove pending property from data fetching composables
This commit is contained in:
parent
9766421739
commit
65be561a37
@ -34,7 +34,7 @@ Nuxt auto-imports functions and composables to perform [data fetching](/docs/get
|
|||||||
```vue
|
```vue
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
/* useAsyncData() and $fetch() are auto-imported */
|
/* useAsyncData() and $fetch() are auto-imported */
|
||||||
const { data, refresh, pending } = await useFetch('/api/hello')
|
const { data, refresh } = await useFetch('/api/hello')
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ Within your pages, components, and plugins you can use useAsyncData to get acces
|
|||||||
|
|
||||||
```vue [pages/index.vue]
|
```vue [pages/index.vue]
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { data, pending, error, refresh } = await useAsyncData(
|
const { data, error, refresh } = await useAsyncData(
|
||||||
'mountains',
|
'mountains',
|
||||||
() => $fetch('https://api.nuxtjs.dev/mountains')
|
() => $fetch('https://api.nuxtjs.dev/mountains')
|
||||||
)
|
)
|
||||||
@ -26,7 +26,7 @@ const { data, pending, error, refresh } = await useAsyncData(
|
|||||||
```
|
```
|
||||||
|
|
||||||
::callout
|
::callout
|
||||||
`data`, `pending`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
|
`data`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
|
||||||
::
|
::
|
||||||
|
|
||||||
### Watch Params
|
### Watch Params
|
||||||
@ -88,7 +88,6 @@ Learn how to use `transform` and `getCachedData` to avoid superfluous calls to a
|
|||||||
## Return Values
|
## Return Values
|
||||||
|
|
||||||
- `data`: the result of the asynchronous function that is passed in.
|
- `data`: the result of the asynchronous function that is passed in.
|
||||||
- `pending`: a boolean indicating whether the data is still being fetched.
|
|
||||||
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
||||||
- `error`: an error object if the data fetching failed.
|
- `error`: an error object if the data fetching failed.
|
||||||
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
||||||
@ -127,7 +126,6 @@ type AsyncDataOptions<DataT> = {
|
|||||||
|
|
||||||
type AsyncData<DataT, ErrorT> = {
|
type AsyncData<DataT, ErrorT> = {
|
||||||
data: Ref<DataT | null>
|
data: Ref<DataT | null>
|
||||||
pending: Ref<boolean>
|
|
||||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
error: Ref<ErrorT | null>
|
error: Ref<ErrorT | null>
|
||||||
|
@ -19,21 +19,21 @@ It automatically generates a key based on URL and fetch options, provides type h
|
|||||||
|
|
||||||
```vue [pages/modules.vue]
|
```vue [pages/modules.vue]
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { data, pending, error, refresh } = await useFetch('/api/modules', {
|
const { data, error, refresh } = await useFetch('/api/modules', {
|
||||||
pick: ['title']
|
pick: ['title']
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
::callout
|
::callout
|
||||||
`data`, `pending`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
|
`data`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
|
||||||
::
|
::
|
||||||
|
|
||||||
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
|
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const param1 = ref('value1')
|
const param1 = ref('value1')
|
||||||
const { data, pending, error, refresh } = await useFetch('/api/modules', {
|
const { data, error, refresh } = await useFetch('/api/modules', {
|
||||||
query: { param1, param2: 'value2' }
|
query: { param1, param2: 'value2' }
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -43,7 +43,7 @@ The above example results in `https://api.nuxt.com/modules?param1=value1¶m2=
|
|||||||
You can also use [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interceptors):
|
You can also use [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interceptors):
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
|
const { data, error, refresh } = await useFetch('/api/auth/login', {
|
||||||
onRequest({ request, options }) {
|
onRequest({ request, options }) {
|
||||||
// Set the request headers
|
// Set the request headers
|
||||||
options.headers = options.headers || {}
|
options.headers = options.headers || {}
|
||||||
@ -114,7 +114,6 @@ Learn how to use `transform` and `getCachedData` to avoid superfluous calls to a
|
|||||||
## Return Values
|
## Return Values
|
||||||
|
|
||||||
- `data`: the result of the asynchronous function that is passed in.
|
- `data`: the result of the asynchronous function that is passed in.
|
||||||
- `pending`: a boolean indicating whether the data is still being fetched.
|
|
||||||
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
||||||
- `error`: an error object if the data fetching failed.
|
- `error`: an error object if the data fetching failed.
|
||||||
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
||||||
@ -155,7 +154,6 @@ type UseFetchOptions<DataT> = {
|
|||||||
|
|
||||||
type AsyncData<DataT, ErrorT> = {
|
type AsyncData<DataT, ErrorT> = {
|
||||||
data: Ref<DataT | null>
|
data: Ref<DataT | null>
|
||||||
pending: Ref<boolean>
|
|
||||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
error: Ref<ErrorT | null>
|
error: Ref<ErrorT | null>
|
||||||
|
@ -25,7 +25,7 @@ By default, [`useAsyncData`](/docs/api/composables/use-async-data) blocks naviga
|
|||||||
/* Navigation will occur before fetching is complete.
|
/* Navigation will occur before fetching is complete.
|
||||||
Handle pending and error states directly within your component's template
|
Handle pending and error states directly within your component's template
|
||||||
*/
|
*/
|
||||||
const { pending, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
||||||
|
|
||||||
watch(count, (newCount) => {
|
watch(count, (newCount) => {
|
||||||
// Because count might start out null, you won't have access
|
// Because count might start out null, you won't have access
|
||||||
@ -35,7 +35,7 @@ watch(count, (newCount) => {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
{{ pending ? 'Loading' : count }}
|
{{ status === 'pending' ? 'Loading' : count }}
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
@ -25,7 +25,7 @@ By default, [`useFetch`](/docs/api/composables/use-fetch) blocks navigation unti
|
|||||||
/* Navigation will occur before fetching is complete.
|
/* Navigation will occur before fetching is complete.
|
||||||
Handle pending and error states directly within your component's template
|
Handle pending and error states directly within your component's template
|
||||||
*/
|
*/
|
||||||
const { pending, data: posts } = await useLazyFetch('/api/posts')
|
const { status, data: posts } = await useLazyFetch('/api/posts')
|
||||||
watch(posts, (newPosts) => {
|
watch(posts, (newPosts) => {
|
||||||
// Because posts might start out null, you won't have access
|
// Because posts might start out null, you won't have access
|
||||||
// to its contents immediately, but you can watch it.
|
// to its contents immediately, but you can watch it.
|
||||||
@ -33,7 +33,7 @@ watch(posts, (newPosts) => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div v-if="pending">
|
<div v-if="status === 'pending'">
|
||||||
Loading ...
|
Loading ...
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: 'clearNuxtData'
|
title: 'clearNuxtData'
|
||||||
description: Delete cached data, error status and pending promises of useAsyncData and useFetch.
|
description: Delete cached data and error status promises of useAsyncData and useFetch.
|
||||||
links:
|
links:
|
||||||
- label: Source
|
- label: Source
|
||||||
icon: i-simple-icons-github
|
icon: i-simple-icons-github
|
||||||
|
@ -58,13 +58,13 @@ This example below refreshes only data where the key matches to `count`.
|
|||||||
|
|
||||||
```vue [pages/some-page.vue]
|
```vue [pages/some-page.vue]
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { pending, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
||||||
const refresh = () => refreshNuxtData('count')
|
const refresh = () => refreshNuxtData('count')
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
{{ pending ? 'Loading' : count }}
|
{{ status === 'pending' ? 'Loading' : count }}
|
||||||
</div>
|
</div>
|
||||||
<button @click="refresh">Refresh</button>
|
<button @click="refresh">Refresh</button>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
Reference in New Issue
Block a user