mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
docs: add status
detail and enhance getCachedData
readability (#30536)
This commit is contained in:
parent
650b83f3ab
commit
f357622fec
@ -69,7 +69,13 @@ const { data: posts } = await useAsyncData(
|
|||||||
- `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
- `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
||||||
- `default`: 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
|
- `default`: 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
|
||||||
- `transform`: a function that can be used to alter `handler` function result after resolving
|
- `transform`: a function that can be used to alter `handler` function result after resolving
|
||||||
- `getCachedData`: Provide a function which returns cached data. A _null_ or _undefined_ return value will trigger a fetch. By default, this is: `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]`, which only caches data when `payloadExtraction` is enabled.
|
- `getCachedData`: Provide a function which returns cached data. A `null` or `undefined` return value will trigger a fetch. By default, this is:
|
||||||
|
```ts
|
||||||
|
const getDefaultCachedData = (key) => nuxtApp.isHydrating
|
||||||
|
? nuxtApp.payload.data[key]
|
||||||
|
: nuxtApp.static.data[key]
|
||||||
|
```
|
||||||
|
Which only caches data when `experimental.payloadExtraction` of `nuxt.config` is enabled.
|
||||||
- `pick`: only pick specified keys in this array from the `handler` function result
|
- `pick`: only pick specified keys in this array from the `handler` function result
|
||||||
- `watch`: watch reactive sources to auto-refresh
|
- `watch`: watch reactive sources to auto-refresh
|
||||||
- `deep`: return data in a deep ref object. It is `false` by default to return data in a shallow ref object for performance.
|
- `deep`: return data in a deep ref object. It is `false` by default to return data in a shallow ref object for performance.
|
||||||
@ -94,7 +100,13 @@ Learn how to use `transform` and `getCachedData` to avoid superfluous calls to a
|
|||||||
- `data`: the result of the asynchronous function that is passed in.
|
- `data`: the result of the asynchronous function that is passed in.
|
||||||
- `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`: when the request has not started, such as:
|
||||||
|
- when `execute` has not yet been called and `{ immediate: false }` is set
|
||||||
|
- when rendering HTML on the server and `{ server: false }` is set
|
||||||
|
- `pending`: the request is in progress
|
||||||
|
- `success`: the request has completed successfully
|
||||||
|
- `error`: the request has failed
|
||||||
- `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.
|
- `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.
|
||||||
|
|
||||||
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
||||||
|
@ -109,7 +109,13 @@ All fetch options can be given a `computed` or `ref` value. These will be watche
|
|||||||
- `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
- `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
||||||
- `default`: 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
|
- `default`: 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
|
||||||
- `transform`: a function that can be used to alter `handler` function result after resolving
|
- `transform`: a function that can be used to alter `handler` function result after resolving
|
||||||
- `getCachedData`: Provide a function which returns cached data. A _null_ or _undefined_ return value will trigger a fetch. By default, this is: `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]`, which only caches data when `payloadExtraction` is enabled.
|
- `getCachedData`: Provide a function which returns cached data. A `null` or `undefined` return value will trigger a fetch. By default, this is:
|
||||||
|
```ts
|
||||||
|
const getDefaultCachedData = (key) => nuxtApp.isHydrating
|
||||||
|
? nuxtApp.payload.data[key]
|
||||||
|
: nuxtApp.static.data[key]
|
||||||
|
```
|
||||||
|
Which only caches data when `experimental.payloadExtraction` of `nuxt.config` is enabled.
|
||||||
- `pick`: only pick specified keys in this array from the `handler` function result
|
- `pick`: only pick specified keys in this array from the `handler` function result
|
||||||
- `watch`: watch an array of reactive sources and auto-refresh the fetch result when they change. Fetch options and URL are watched by default. You can completely ignore reactive sources by using `watch: false`. Together with `immediate: false`, this allows for a fully-manual `useFetch`. (You can [see an example here](/docs/getting-started/data-fetching#watch) of using `watch`.)
|
- `watch`: watch an array of reactive sources and auto-refresh the fetch result when they change. Fetch options and URL are watched by default. You can completely ignore reactive sources by using `watch: false`. Together with `immediate: false`, this allows for a fully-manual `useFetch`. (You can [see an example here](/docs/getting-started/data-fetching#watch) of using `watch`.)
|
||||||
- `deep`: return data in a deep ref object. It is `false` by default to return data in a shallow ref object for performance.
|
- `deep`: return data in a deep ref object. It is `false` by default to return data in a shallow ref object for performance.
|
||||||
@ -134,7 +140,13 @@ Learn how to use `transform` and `getCachedData` to avoid superfluous calls to a
|
|||||||
- `data`: the result of the asynchronous function that is passed in.
|
- `data`: the result of the asynchronous function that is passed in.
|
||||||
- `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`: when the request has not started, such as:
|
||||||
|
- when `execute` has not yet been called and `{ immediate: false }` is set
|
||||||
|
- when rendering HTML on the server and `{ server: false }` is set
|
||||||
|
- `pending`: the request is in progress
|
||||||
|
- `success`: the request has completed successfully
|
||||||
|
- `error`: the request has failed
|
||||||
- `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.
|
- `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.
|
||||||
|
|
||||||
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
||||||
|
Loading…
Reference in New Issue
Block a user