Skip to content

Commit

Permalink
Introduce the useFetch() sample code (with API endpoint)
Browse files Browse the repository at this point in the history
  • Loading branch information
LSViana committed Oct 26, 2024
1 parent ee4973d commit d98d106
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pages/experiments/data-fetching/nuxt/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@
<WlExperimentCanvas>
<p><code>$fetch()</code> = <code>{{ data.fetch }}</code></p>
</WlExperimentCanvas>
<h2 class="mt-3 text-lg"><code>useFetch()</code></h2>
<p>Fetches data both only on server (unless client-side navigation happens):</p>
<WlExperimentCanvas>
<p><code>useFetch()</code> = <code>{{ data.useFetch }}</code></p>
</WlExperimentCanvas>
</WlContainer>
</NuxtLayout>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useFetch } from '#app'
import WlExperimentCanvas from '~/components/shared/experiments/WlExperimentCanvas.vue'
import WlContainer from '~/components/shared/layout/WlContainer.vue'
const data = reactive({
fetch: 0
fetch: 0,
useFetch: 0
})
const fetchResponse = await $fetch('/api/data-fetching/nuxt/fetch')
data.fetch = fetchResponse.value
const useFetchResponse = await useFetch('/api/data-fetching/nuxt/use-fetch')
data.useFetch = useFetchResponse.data.value!.value
</script>

9 changes: 9 additions & 0 deletions server/api/data-fetching/nuxt/use-fetch.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineEventHandler } from 'h3'

export default defineEventHandler(async () => {
console.log(`[${new Date().toISOString()}] API - useFetch`)

return {
value: 2
}
})

0 comments on commit d98d106

Please sign in to comment.