Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
53 captures
16 Nov 2022 - 21 Sep 2025
JunJULAug
02
202220232024
success
fail
COLLECTED BY
TIMESTAMPS
loading
The Wayback Machine - http://web.archive.org/web/20230702231323/https://nuxt.com/docs/api/composables/use-async-data
✨ Discover our official Nuxt Training Courses.Learn more

useAsyncData

Within your pages, components, and plugins you can use useAsyncData to get access to data that resolves asynchronously.

useAsyncData is a composable meant to be called directly in a setup function, plugin, or route middleware. It returns reactive composables and handles adding responses to the Nuxt payload so they can be passed from server to client without re-fetching the data on client side when the page hydrates.

Type

Signature
functionuseAsyncData(handler: (nuxtApp?:NuxtApp)=>Promise<DataT>,options?:AsyncDataOptions<DataT>):AsyncData<DataT>functionuseAsyncData(key:string,handler: (nuxtApp?:NuxtApp)=>Promise<DataT>,options?:AsyncDataOptions<DataT>):Promise<AsyncData<DataT>>typeAsyncDataOptions<DataT>= {server?:booleanlazy?:booleandefault?: ()=>DataT|Ref<DataT>|nulltransform?: (input:DataT)=>DataTpick?:string[]watch?:WatchSource[]immediate?:boolean}typeAsyncData<DataT,ErrorT>= {data:Ref<DataT|null>pending:Ref<boolean>refresh: (opts?:AsyncDataExecuteOptions)=>Promise<void>execute: (opts?:AsyncDataExecuteOptions)=>Promise<void>error:Ref<ErrorT|null>status:Ref<AsyncDataRequestStatus>};interfaceAsyncDataExecuteOptions {dedupe?:boolean}typeAsyncDataRequestStatus='idle'|'pending'|'success'|'error'

Params

  • key: a unique key to ensure that data fetching can be properly de-duplicated across requests. If you do not provide a key, then a key that is unique to the file name and line number of the instance ofuseAsyncData will be generated for you.
  • handler: an asynchronous function that returns a value
  • options:
    • lazy: whether to resolve the async function after loading the route, instead of blocking client-side navigation (defaults tofalse)
    • default: a factory function to set the default value of the data, before the async function resolves - particularly useful with thelazy: true option
    • server: whether to fetch the data on the server (defaults totrue)
    • transform: a function that can be used to alterhandler function result after resolving
    • pick: only pick specified keys in this array from thehandler function result
    • watch: watch reactive sources to auto-refresh
    • immediate: When set tofalse, will prevent the request from firing immediately. (defaults totrue)

Under the hood,lazy: false uses<Suspense> to block the loading of the route before the data has been fetched. Consider usinglazy: true and implementing a loading state instead for a snappier user experience.

Return Values

  • 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 thehandler function.
  • error: an error object if the data fetching failed.
  • status: a string indicating the status of the data request ("idle","pending","success","error").

By default, Nuxt waits until arefresh is finished before it can be executed again.

If you have not fetched data on the server (for example, withserver: false), then the datawill not be fetched until hydration completes. This means even if you awaituseAsyncData on the client side,data will remainnull within<script setup>.

Example

const {data,pending,error,refresh }=awaituseAsyncData('mountains',  ()=>$fetch('https://api.nuxtjs.dev/mountains'))

Example with watching params change

The built-inwatch option allows automatically rerunning the fetcher function when any changes are detected.

constpage=ref(1)const {data:posts }=awaituseAsyncData('posts',  ()=>$fetch('https://fakeApi.com/posts', {params: {page:page.value    }  }), {watch: [page]  })

useAsyncData is a reserved function name transformed by the compiler, so you should not name your own functionuseAsyncData.


[8]ページ先頭

©2009-2025 Movatter.jp