docs(api): add ts signature and js example to use-async-data (#4171)

Co-authored-by: Sébastien Chopin <seb@nuxtjs.com>
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Clément Ollivier 2022-04-11 19:24:18 +02:00 committed by GitHub
parent d6210606bf
commit e6471689d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,23 +1,32 @@
# `useAsyncData`
::ReadMore{link="/guide/features/data-fetching"}
::
Within your pages, components, and plugins you can use useAsyncData to get access to data that resolves asynchronously.
```ts
const {
data: Ref<DataT>,
pending: Ref<boolean>,
refresh: () => Promise<void>,
error: Ref<any>
} = useAsyncData(
## Type
```ts [Signature]
function useAsyncData(
key: string,
handler: (ctx?: NuxtApp) => Promise<Object>,
options?: {
lazy: boolean,
server: boolean,
watch: WatchSource[]
}
)
handler: (nuxtApp?: NuxtApp) => Promise<DataT>,
options?: AsyncDataOptions
): Promise<DataT>
type AsyncDataOptions = {
server?: boolean
lazy?: boolean
default?: () => DataT
transform?: (input: DataT) => DataT
pick?: string[]
watch?: WatchSource[]
initialCache?: boolean
}
type DataT = {
data: Ref<DataT>
pending: Ref<boolean>
refresh: () => Promise<void>
error: Ref<any>
}
```
## Params
@ -43,3 +52,18 @@ Under the hood, `lazy: false` uses `<Suspense>` to block the loading of the rout
* **error**: an error object if the data fetching failed
By default, Nuxt waits until a `refresh` is finished before it can be executed again. Passing `true` as parameter skips that wait.
## Example
```ts
const { data, pending, error, refresh } = useAsyncData(
'mountains',
() => $fetch('https://api.nuxtjs.dev/mountains),
{
pick: ['title']
}
)
```
::ReadMore{link="/guide/features/data-fetching"}
::