Skip to content

Commit 82b7cf7

Browse files
docs: translate content form Japanese to English (#214)
* docs: translate content form Japanese to English * update style of folder structure * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 82f13de commit 82b7cf7

File tree

27 files changed

+509
-19
lines changed

27 files changed

+509
-19
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
const { data: todos, refresh } = useFetch('/api/todos')
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="refresh()">
7+
Refresh
8+
</button>
9+
10+
<ul>
11+
<li v-for="todo in todos" :key="todo.id">
12+
{{ todo.title }}
13+
</li>
14+
</ul>
15+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default defineEventHandler(() => {
2+
return [
3+
{ id: 1, title: 'Todo 1' },
4+
{ id: 2, title: 'Todo 2' },
5+
{ id: 3, title: 'Todo 3' },
6+
]
7+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { GuideMeta } from '~/types/guides'
2+
3+
export const meta: GuideMeta = {
4+
startingFile: 'app.vue',
5+
features: {
6+
fileTree: true,
7+
},
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script setup lang="ts">
2+
const { data: todos, refresh } = useFetch('/api/todos')
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="refresh()">
7+
Refresh
8+
</button>
9+
10+
<ul>
11+
<li v-for="todo in todos" :key="todo.id">
12+
<span :class="{ completed: todo.completed }">{{ todo.title }}</span>
13+
</li>
14+
</ul>
15+
</template>
16+
17+
<style scoped>
18+
.completed {
19+
text-decoration: line-through;
20+
}
21+
</style>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default defineEventHandler(() => {
2+
return [
3+
{ id: 1, title: 'Todo 1', completed: false },
4+
{ id: 2, title: 'Todo 2', completed: false },
5+
{ id: 3, title: 'Todo 3', completed: true },
6+
{ id: 4, title: 'Todo 4', completed: false },
7+
]
8+
})

content/2.concepts/10.data-fetching/index.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,52 @@ ogImage: true
44

55
# Data Fetching
66

7-
<!-- TODO:
8-
- What is Data Fetching?
9-
- useFetch, $fetch, useAsyncData
10-
- SSR (default)
11-
- CSR
12-
- SSG
13-
- ISR (?)
14-
-->
7+
When building practical applications, data fetching is an essential feature.\
8+
Data fetching refers to retrieving data from APIs or databases.
9+
10+
Nuxt provides useful functions like `useFetch`, `useAsyncData`, and `$fetch` to handle data fetching conveniently.
11+
12+
In short:
13+
14+
- `useFetch` is the simplest way to handle data fetching in a component's setup function.
15+
- `$fetch` is ideal for making network requests based on user interactions.
16+
- `useAsyncData` works with `$fetch` to provide more granular control.
17+
18+
https://nuxt.com/docs/getting-started/data-fetching
19+
20+
Among these, `useFetch` is the easiest to use. In fact, it is a convenient wrapper around `useAsyncData` and `$fetch`.
21+
22+
Here’s how to use it:
23+
24+
```vue
25+
<script setup lang="ts">
26+
const { data, pending, error, refresh, clear } = await useFetch('/api/modules')
27+
</script>
28+
```
29+
30+
Specifically, it offers the following features:
31+
32+
- Runs on both server and client\
33+
Since `useFetch` works on both the server and client sides, it allows easy data fetching even during universal rendering.
34+
- Data caching\
35+
When the API is called on the server, the data is transferred to the client, preventing redundant fetching on the client side.
36+
- Typed request URLs and responses\
37+
By implementing the API inside the `server` directory, request URLs and response types are automatically inferred.
38+
39+
For more details, refer to the [official documentation](https://nuxt.com/docs/api/composables/use-fetch).
40+
41+
If you need finer control, you can use `useAsyncData` or `$fetch` for more advanced data fetching.
42+
43+
https://nuxt.com/docs/api/composables/use-async-data
44+
45+
https://nuxt.com/docs/api/utils/dollarfetch
46+
47+
## Challenge
48+
49+
1. Check that the API works
50+
Add a fourth Todo item to `server/api/todos/index.ts`, then click the refresh button to verify that the data updates.
51+
52+
2. Check type inference
53+
Add a `completed` property to the Todo in `server/api/todos/index.ts`, and confirm that the `useFetch` type updates accordingly.
54+
55+
:ButtonShowSolution{.bg-faded.px4.py2.mb3.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<NuxtLayout>
3+
<NuxtPage />
4+
</NuxtLayout>
5+
</template>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<div class="view">
3+
<header>
4+
header
5+
</header>
6+
<main>
7+
<slot />
8+
</main>
9+
<footer>
10+
footer
11+
</footer>
12+
</div>
13+
</template>
14+
15+
<style scoped>
16+
.view {
17+
display: flex;
18+
flex-direction: column;
19+
height: 100vh;
20+
width: 100vw;
21+
}
22+
23+
header {
24+
background-color: #333;
25+
color: white;
26+
padding: 1rem;
27+
}
28+
29+
main {
30+
flex: 1;
31+
padding: 1rem;
32+
}
33+
34+
footer {
35+
background-color: #333;
36+
color: white;
37+
padding: 1rem;
38+
}
39+
</style>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<template>
2+
<div class="view">
3+
<main>
4+
<slot />
5+
</main>
6+
<footer>
7+
footer
8+
</footer>
9+
</div>
10+
</template>
11+
12+
<style scoped>
13+
.view {
14+
display: flex;
15+
flex-direction: column;
16+
height: 100vh;
17+
width: 100vw;
18+
}
19+
20+
main {
21+
flex: 1;
22+
padding: 1rem;
23+
}
24+
25+
footer {
26+
background-color: #333;
27+
color: white;
28+
padding: 1rem;
29+
}
30+
</style>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup lang="ts">
2+
definePageMeta({
3+
layout: 'no-header',
4+
})
5+
</script>
6+
7+
<template>
8+
<h1>Foo</h1>
9+
<NuxtLink to="/">
10+
/Index
11+
</NuxtLink>
12+
</template>

0 commit comments

Comments
 (0)