@@ -156,7 +156,7 @@ transition: fade-out
156156# Fetch
157157
158158- ` $fetch() ` ist isometrisches ` fetch() `
159- - für einmalige Requests wie z.B. beim Klick auf einem Button
159+ - für einmalige Requests wie z.B. beim Klick auf einen Button
160160- für Requests auf Serverseite
161161- kein Auto-caching oder Request-deduplication von Nuxt
162162 - mehrere gleiche & gleichzeitige Requests würden alle ausgeführt werden
@@ -171,38 +171,41 @@ return await $fetch("https://api.example.invalid/users/12", {
171171
172172---
173173transition: fade-out
174- layout: two-column
175174---
176175
177176# Fetch
178177
179178<div class =" grid grid-cols-2 gap-sm " >
180179
181- - ` useAsyncData() ` wie früher ` asyncData `
182- - Wenn Daten nicht unbedingt von einer API kommen
183- - Meist in Pages (oder auch Components & Composables)
184- - Für komplexes Fetchen
185- - mit Auto-caching und Request-deduplication von Nuxt
180+ - ` useAsyncData() ` -Composable wie früher ` asyncData `
181+ - Nur im Vue-Context
182+ - Für komplexes Fetchen (oder andere async. Logik)
183+ - Mit Auto-caching und Request-deduplication von Nuxt
186184 - mehrere gleiche & gleichzeitige Requests würden nur einmal ausgeführt werden
187185
188186``` vue
189187<script setup>
190- const { data: products, pending, error } = await useAsyncData(
191- 'products', // Unique key
192- async () => {
193- // Custom fetch logic
194- const response = await $fetch('/api/products')
195- return response.items.filter(item => item.inStock)
196- },
197- {
198- // Optional: refresh on route changes
199- watch: [/* reactive dependencies */],
200- // Optional: server-side only
201- server: true,
202- // Optional: lazy loading
203- lazy: false
204- }
205- )
188+ const { data, status, error, refresh, clear } =
189+ await useAsyncData(
190+ 'user', // Optional: Unique key
191+ async () => {
192+ // Custom fetch logic
193+ const [posts, followers, activity] =
194+ await Promise.all([
195+ $fetch(`/api/users/${userId.value}/posts`),
196+ $fetch(`/api/users/${userId.value}/followers`),
197+ $fetch(`/api/users/${userId.value}/activity`)
198+ ])
199+ return { posts, followers, activity }
200+ },
201+ {
202+ // Optional: refresh on route changes
203+ watch: [/* reactive dependencies */],
204+ // Optional: lazy loading
205+ lazy: false
206+ // ...
207+ }
208+ )
206209</script>
207210```
208211
@@ -212,6 +215,68 @@ const { data: products, pending, error } = await useAsyncData(
212215transition: fade-out
213216---
214217
218+ # Fetch
219+
220+ <div class =" grid grid-cols-3 gap-sm " >
221+
222+ <div class =" col-span-1 " >
223+
224+ - ` useFetch() ` -Composable kombiniert ` useAsyncData() ` & ` $fetch() `
225+ - Nur im Vue-Context
226+ - Nur für HTTP-Requests
227+ - Simpler & automatisch reaktiv
228+
229+ </div >
230+ <div class =" col-span-2 " >
231+
232+ ``` vue
233+ <script setup>
234+ const page = ref(1)
235+ const pageSize = ref(10)
236+
237+ const { data: photos, pending, error, refresh } =
238+ await useFetch("/api/photos", {
239+ // Optional: set a key for caching
240+ key: `photos-${toValue(page)}-${toValue(pageSize)}`,
241+ // Optional: set query params
242+ params: { page, pageSize },
243+ // Optional: transform the response
244+ transform: (response) => response.photos,
245+ // ...
246+ })
247+
248+ function nextPage() {
249+ page.value++
250+ }
251+ </script>
252+ ```
253+
254+ </div >
255+ </div >
256+
257+ ---
258+ transition: fade-out
259+ layout: items
260+ ---
261+
262+ # Fetch
263+
264+ Wann was nutzen?
265+
266+ ``` mermaid
267+ flowchart LR
268+ A[Bin ich auf dem Server?] -->|Ja| B["$fetch()"]
269+ A -->|Nein| C[Mache ich einmalige Anfragen, die **kein** Auto-Caching und Request-Deduplication benötigen?]
270+ C -->|Ja| B
271+ C -->|Nein| E["Brauche ich flexiblen low-level Zugriff auf komplexe asynchrone Operationen (mehr als nur HTTP)?"]
272+ E -->|Ja| F["useAsyncData()"]
273+ E -->|Nein| G["useFetch()"]
274+ ```
275+
276+ ---
277+ transition: fade-out
278+ ---
279+
215280# Exception Handling
216281
217282- ` error.vue ` -Page bei fatalem Error
@@ -263,7 +328,29 @@ transition: fade-out
263328
264329# Exception Handling
265330
331+ - Vues ` onErrorCaptured() ` -Composable zum programmatischen Catchen von Errors
332+
333+ ``` vue
334+ <script setup>
335+ onErrorCaptured((error, instance, info) => {
336+ // Global error logging that catches everything
337+ console.error(`Global error: ${error.message}`)
338+ console.log(`Error occurred in component:`, instance)
339+ console.log(`Error info:`, info)
340+
341+ // Let the error continue to propagate to UI boundaries
342+ return true
343+ })
344+ </script>
345+ ```
346+ ---
347+ transition: fade-out
348+ ---
349+
350+ # Exception Handling
351+
266352- ` <NuxtErrorBoundary> ` zum Catchen von Errors im UI
353+ - Nutzt ` onErrorCaptured() ` -Composable intern
267354
268355``` vue
269356<template>
@@ -281,36 +368,26 @@ transition: fade-out
281368
282369---
283370transition: fade-out
371+ layout: items
284372---
285373
286374# Exception Handling
287375
288- - Vues ` onErrorCaptured() ` -Composable zum programmatischen Catchen von Errors
289-
290- ``` vue
291- <script setup>
292- onErrorCaptured((error, instance, info) => {
293- // Global error logging that catches everything
294- console.error(`Global error: ${error.message}`)
295- console.log(`Error occurred in component:`, instance)
296- console.log(`Error info:`, info)
376+ Wann was verwenden?
297377
298- // Let the error continue to propagate to UI boundaries
299- return true
300- })
301- </script>
378+ ``` mermaid
379+ flowchart LR
380+ A["Ist mein Error fatal (Server-Error oder createError() mit fatal: true)?"]
381+ B["*error.vue*-Page"]
382+ C["Möchte ich globales, komplexes, programmatisches oder UI-unabhängiges Error-Handling?"]
383+ D["onErrorCaptured()"]
384+ E["NuxtErrorBoundary-Component"]
385+
386+ A -->|Ja| B
387+ A -->|Nein| C
388+ C -->|Ja| D
389+ C -->|Nein| E
302390```
303- ---
304- transition: fade-out
305- ---
306-
307- # Exception Handling
308-
309- Wann was verwenden?
310- - ` <NuxtErrorBoundary> ` : UI-spezifisches Error-Handling
311- - ` onErrorCaptured() ` : globales oder komplexes Error-Handling, UI-unabhängig, kann Propagation stoppen
312- - ` error.vue ` -Page, wenn fataler Error auftritt
313- - Server-Error oder ` createError("...", { fatal: true }) `
314391
315392---
316393transition: fade-out
0 commit comments