@nuxt/docs-nightly 4.1.3-29313143.14514329 → 4.1.3-29313364.98ecc620
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -481,6 +481,21 @@ const { data, error, refresh } = await useFetch(`/api/users/${id.value}`, {
|
|
|
481
481
|
|
|
482
482
|
If you need to change the URL based on a reactive value, you may want to use a [computed URL](/docs/getting-started/data-fetching#computed-url) instead.
|
|
483
483
|
|
|
484
|
+
When reactive fetch options are provided, they'll be automatically watched and trigger refetches. In some cases, it can be useful to opt-out of this behavior by specifying `watch: false`.
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
const id = ref(1)
|
|
488
|
+
|
|
489
|
+
// Won't automatically refetch when id changes
|
|
490
|
+
const { data, execute } = await useFetch('/api/users', {
|
|
491
|
+
query: { id }, // id is watched by default
|
|
492
|
+
watch: false, // disables automatic watching of id
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
// doesn't trigger refetch
|
|
496
|
+
id.value = 2
|
|
497
|
+
```
|
|
498
|
+
|
|
484
499
|
#### Computed URL
|
|
485
500
|
|
|
486
501
|
Sometimes you may need to compute a URL from reactive values, and refresh the data each time these change. Instead of juggling your way around, you can attach each param as a reactive value. Nuxt will automatically use the reactive value and re-fetch each time it changes.
|
|
@@ -98,6 +98,31 @@ If you encounter the `data` variable destructured from a `useFetch` returns a st
|
|
|
98
98
|
|
|
99
99
|
:read-more{to="/docs/getting-started/data-fetching"}
|
|
100
100
|
|
|
101
|
+
### Reactive Fetch Options
|
|
102
|
+
|
|
103
|
+
Fetch options can be provided as reactive, supporting `computed`, `ref` and [computed getters](https://vuejs.org/guide/essentials/computed.html). When a reactive fetch option is updated it will trigger a refetch using the updated resolved reactive value.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const searchQuery = ref('initial')
|
|
107
|
+
const { data } = await useFetch('/api/search', {
|
|
108
|
+
query: { q: searchQuery }
|
|
109
|
+
})
|
|
110
|
+
// triggers a refetch: /api/search?q=new%20search
|
|
111
|
+
searchQuery.value = 'new search'
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
If needed, you can opt out of this behavior using `watch: false`:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const searchQuery = ref('initial')
|
|
118
|
+
const { data } = await useFetch('/api/search', {
|
|
119
|
+
query: { q: searchQuery },
|
|
120
|
+
watch: false
|
|
121
|
+
})
|
|
122
|
+
// does not trigger a refetch
|
|
123
|
+
searchQuery.value = 'new search'
|
|
124
|
+
```
|
|
125
|
+
|
|
101
126
|
## Type
|
|
102
127
|
|
|
103
128
|
```ts [Signature]
|
|
@@ -108,12 +133,12 @@ function useFetch<DataT, ErrorT>(
|
|
|
108
133
|
|
|
109
134
|
type UseFetchOptions<DataT> = {
|
|
110
135
|
key?: MaybeRefOrGetter<string>
|
|
111
|
-
method?: string
|
|
112
|
-
query?: SearchParams
|
|
113
|
-
params?: SearchParams
|
|
114
|
-
body?: RequestInit['body'] | Record<string, any
|
|
115
|
-
headers?: Record<string, string> | [key: string, value: string][] | Headers
|
|
116
|
-
baseURL?: string
|
|
136
|
+
method?: MaybeRefOrGetter<string>
|
|
137
|
+
query?: MaybeRefOrGetter<SearchParams>
|
|
138
|
+
params?: MaybeRefOrGetter<SearchParams>
|
|
139
|
+
body?: MaybeRefOrGetter<RequestInit['body'] | Record<string, any>>
|
|
140
|
+
headers?: MaybeRefOrGetter<Record<string, string> | [key: string, value: string][] | Headers>
|
|
141
|
+
baseURL?: MaybeRefOrGetter<string>
|
|
117
142
|
server?: boolean
|
|
118
143
|
lazy?: boolean
|
|
119
144
|
immediate?: boolean
|
|
@@ -125,6 +150,7 @@ type UseFetchOptions<DataT> = {
|
|
|
125
150
|
pick?: string[]
|
|
126
151
|
$fetch?: typeof globalThis.$fetch
|
|
127
152
|
watch?: MultiWatchSources | false
|
|
153
|
+
timeout?: MaybeRefOrGetter<number>
|
|
128
154
|
}
|
|
129
155
|
|
|
130
156
|
type AsyncDataRequestContext = {
|
|
@@ -157,13 +183,13 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
157
183
|
| Option | Type | Default | Description |
|
|
158
184
|
| ---| --- | --- | --- |
|
|
159
185
|
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from URL and options. |
|
|
160
|
-
| `method` | `string
|
|
161
|
-
| `query` | `
|
|
162
|
-
| `params` | `
|
|
163
|
-
| `body` | `RequestInit['body'] \| Record<string, any
|
|
164
|
-
| `headers` | `Record<string, string> \| [key, value][] \| Headers
|
|
165
|
-
| `baseURL` | `string
|
|
166
|
-
| `timeout` | `number
|
|
186
|
+
| `method` | `MaybeRefOrGetter<string>` | `'GET'` | HTTP request method. |
|
|
187
|
+
| `query` | `MaybeRefOrGetter<SearchParams>` | - | Query/search params to append to the URL. Alias: `params`. |
|
|
188
|
+
| `params` | `MaybeRefOrGetter<SearchParams>` | - | Alias for `query`. |
|
|
189
|
+
| `body` | `MaybeRefOrGetter<RequestInit['body'] \| Record<string, any>>` | - | Request body. Objects are automatically stringified. |
|
|
190
|
+
| `headers` | `MaybeRefOrGetter<Record<string, string> \| [key, value][] \| Headers>` | - | Request headers. |
|
|
191
|
+
| `baseURL` | `MaybeRefOrGetter<string>` | - | Base URL for the request. |
|
|
192
|
+
| `timeout` | `MaybeRefOrGetter<number>` | - | Timeout in milliseconds to abort the request. |
|
|
167
193
|
| `cache` | `boolean \| string` | - | Cache control. Boolean disables cache, or use Fetch API values: `default`, `no-store`, etc. |
|
|
168
194
|
| `server` | `boolean` | `true` | Whether to fetch on the server. |
|
|
169
195
|
| `lazy` | `boolean` | `false` | If true, resolves after route loads (does not block navigation). |
|
|
@@ -175,7 +201,7 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
175
201
|
| `watch` | `MultiWatchSources \| false` | - | Array of reactive sources to watch and auto-refresh. `false` disables watching. |
|
|
176
202
|
| `deep` | `boolean` | `false` | Return data in a deep ref object. |
|
|
177
203
|
| `dedupe` | `'cancel' \| 'defer'` | `'cancel'` | Avoid fetching same key more than once at a time. |
|
|
178
|
-
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. |
|
|
204
|
+
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. See [Custom useFetch in Nuxt](/docs/guide/recipes/custom-usefetch) |
|
|
179
205
|
|
|
180
206
|
::note
|
|
181
207
|
All fetch options can be given a `computed` or `ref` value. These will be watched and new requests made automatically with any new values if they are updated.
|