useAsyncData is a composable meant to be called directly in the Nuxt context. 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.Usage
[app/pages/index.vue]
data, status, pending and error are Vue refs and they should be accessed with .value when used within the <script setup>, while refresh/execute and clear are plain functions.Parameters
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 of
useAsyncData will be generated for you. Can be reactive for dynamic data fetching.An asynchronous function that must return a truthy value (for example, it should not be
undefined or null) or the request may be duplicated on the client side. The handler function should be side-effect free to ensure predictable behavior during SSR and CSR hydration.Options to configure the behavior of
useAsyncData.Whether to fetch the data on the server.
Whether to resolve the async function after loading the route, instead of blocking client-side navigation.
When set to
false, will prevent the request from firing immediately.A factory function to set the default value of the
data, before the async function resolves - useful with the lazy: true or immediate: false option.A function that can be used to alter
handler function result after resolving.Provide a function which returns cached data. An
undefined return value will trigger a fetch.Only pick specified keys in this array from the
handler function result.Watch reactive sources to auto-refresh. Set to
false to disable watching.Return data in a deep ref object. It is
false by default to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive.Avoid fetching same key more than once at a time.
cancel cancels existing requests when a new one is made. defer does not make new requests at all if there is a pending request.A number in milliseconds to wait before timing out the request.
Return Values
The result of the asynchronous function that is passed in.
A function that can be used to refresh the data returned by the
handler function. Also aliased as execute.An error object if the data fetching failed.
A string indicating the status of the data request:
idle: when the request has not startedpending: the request is in progresssuccess: the request has completed successfullyerror: the request has failed
A boolean that is
true while the request is in progress (that is, while status.value === 'pending').A function that can be used to set
data to undefined, set error to undefined, set status to idle, and mark any currently pending requests as cancelled.Examples
Watch Params
The built-inwatch option allows automatically rerunning the fetcher function when any changes are detected.
[app/pages/index.vue]
Reactive Keys
You can use a computed ref, plain ref or a getter function as the key, allowing for dynamic data fetching that automatically updates when the key changes:[app/pages/[id].vue]
Make your handler abortable
You can make yourhandler function abortable by using the signal provided in the second argument: