mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
feat(nuxt): add clearNuxtData
(#5227)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
1c07914608
commit
b2f573f685
@ -156,7 +156,7 @@ This method is useful if you want to refresh all the data fetching for a current
|
|||||||
::ReadMore{link="/api/utils/refresh-nuxt-data"}
|
::ReadMore{link="/api/utils/refresh-nuxt-data"}
|
||||||
::
|
::
|
||||||
|
|
||||||
### Example
|
#### Example
|
||||||
|
|
||||||
```vue
|
```vue
|
||||||
<template>
|
<template>
|
||||||
@ -173,6 +173,15 @@ const refresh = () => refreshNuxtData('count')
|
|||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `clearNuxtData`
|
||||||
|
|
||||||
|
Delete cached data, error status and pending promises of `useAsyncData` and `useFetch`.
|
||||||
|
|
||||||
|
This method is useful if you want to invalidate the data fetching for another page.
|
||||||
|
|
||||||
|
::ReadMore{link="/api/utils/clear-nuxt-data"}
|
||||||
|
::
|
||||||
|
|
||||||
## Options API support
|
## Options API support
|
||||||
|
|
||||||
Nuxt 3 provides a way to perform `asyncData` fetching within the Options API. You must wrap your component definition within `defineNuxtComponent` for this to work.
|
Nuxt 3 provides a way to perform `asyncData` fetching within the Options API. You must wrap your component definition within `defineNuxtComponent` for this to work.
|
||||||
|
18
docs/content/3.api/3.utils/clear-nuxt-data.md
Normal file
18
docs/content/3.api/3.utils/clear-nuxt-data.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# `clearNuxtData`
|
||||||
|
|
||||||
|
::StabilityEdge
|
||||||
|
::
|
||||||
|
|
||||||
|
Delete cached data, error status and pending promises of `useAsyncData` and `useFetch`.
|
||||||
|
|
||||||
|
This method is useful if you want to invalidate the data fetching for another page.
|
||||||
|
|
||||||
|
## Type
|
||||||
|
|
||||||
|
```ts
|
||||||
|
clearNuxtData (keys?: string | string[]): void
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
* `keys`: On or an array of keys that are used in `useAsyncData` to delete their cached data. If no keys are provided, **every data** will be invalidated.
|
@ -254,6 +254,28 @@ export function refreshNuxtData (keys?: string | string[]): Promise<void> {
|
|||||||
return useNuxtApp().callHook('app:data:refresh', _keys)
|
return useNuxtApp().callHook('app:data:refresh', _keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function clearNuxtData (keys?: string | string[]): void {
|
||||||
|
const nuxtApp = useNuxtApp()
|
||||||
|
const _keys = keys ? Array.from(keys).filter(key => key && typeof key === 'string') : Object.keys(nuxtApp.payload.data)
|
||||||
|
|
||||||
|
for (const key of _keys) {
|
||||||
|
if (key in nuxtApp.payload.data) {
|
||||||
|
nuxtApp.payload.data[key] = undefined
|
||||||
|
}
|
||||||
|
if (key in nuxtApp.payload._errors) {
|
||||||
|
nuxtApp.payload._errors[key] = undefined
|
||||||
|
}
|
||||||
|
if (nuxtApp._asyncData[key]) {
|
||||||
|
nuxtApp._asyncData[key]!.data.value = undefined
|
||||||
|
nuxtApp._asyncData[key]!.error.value = undefined
|
||||||
|
nuxtApp._asyncData[key]!.pending.value = false
|
||||||
|
}
|
||||||
|
if (key in nuxtApp._asyncDataPromises) {
|
||||||
|
nuxtApp._asyncDataPromises[key] = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function pick (obj: Record<string, any>, keys: string[]) {
|
function pick (obj: Record<string, any>, keys: string[]) {
|
||||||
const newObj = {}
|
const newObj = {}
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
|
@ -69,7 +69,7 @@ interface _NuxtApp {
|
|||||||
data: Ref<any>
|
data: Ref<any>
|
||||||
pending: Ref<boolean>
|
pending: Ref<boolean>
|
||||||
error: Ref<any>
|
error: Ref<any>
|
||||||
}>,
|
} | undefined>,
|
||||||
|
|
||||||
ssrContext?: NuxtSSRContext
|
ssrContext?: NuxtSSRContext
|
||||||
payload: {
|
payload: {
|
||||||
|
Loading…
Reference in New Issue
Block a user