Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit c7aceab

Browse files
authored
feat: content/2.concepts/7.data-fetching (#41)
* wip * content/2.concepts/7.data-fetching * fmt
1 parent 39989b3 commit c7aceab

File tree

10 files changed

+8500
-9409
lines changed

10 files changed

+8500
-9409
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/7.data-fetching/index.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,53 @@
22
ogImage: true
33
---
44

5-
# Data Fetching
6-
7-
<!-- TODO:
8-
- What is Data Fetching?
9-
- useFetch, $fetch, useAsyncData
10-
- SSR (default)
11-
- CSR
12-
- SSG
13-
- ISR (?)
14-
-->
5+
# データフェッチ
6+
7+
実用的なアプリケーションを作る上で、データフェッチは欠かせない機能です。
8+
データフェッチとは API やデータベースからデータを取得してくることを指します。
9+
10+
Nuxt では、このデータフェッチを便利に扱うために `useFetch``useAsyncData``$fetch` といった関数を提供しています。
11+
12+
一言で言えば、
13+
14+
- `useFetch` は、コンポーネントのセットアップ関数でデータのフェッチを処理する最も簡単な方法です。
15+
- `fetch` は、ユーザーのインタラクションに基づいてネットワークリクエストを行うのに最適です。
16+
- `useAsyncData` は、`$fetch` と組み合わせることで、よりきめ細かい制御を提供します。
17+
18+
https://nuxt.com/docs/getting-started/data-fetching
19+
20+
中でも、useFetch は最も簡単な方法で、実際には `useAsyncData``$fetch` の便利なラッパーです。
21+
22+
使い方は以下の通りで、
23+
24+
```vue
25+
<script setup lang="ts">
26+
const { data, pending, error, refresh, clear } = await useFetch('/api/modules')
27+
</script>
28+
```
29+
30+
具体的には以下のような機能があります。
31+
32+
- サーバーとクライアントの両方で動作する\
33+
useFetch はサーバーとクライアントの両方で動作することができるので、ユニバーサルレンダリング時でも簡単にデータフェッチを行うことができます。
34+
- データキャッシュ\
35+
サーバー上で API が呼ばれた時、そのデータをクライアントに転送することで、クライアント側で再度データフェッチが行われることを防ぎます。
36+
- リクエスト URL とレスポンスの型付け\
37+
server ディレクトリに API を実装することで、リクエスト URL とレスポンスの型付けが自動的に行われます。
38+
39+
より詳細な使い方については、[公式ドキュメント](https://nuxt.com/docs/api/composables/use-fetch) を参照してください。
40+
41+
また、より細かい制御を行いたい場合は `useAsyncData``$fetch` を利用することで、より高度なデータフェッチを行うことができます。
42+
43+
https://nuxt.com/docs/api/composables/use-async-data
44+
45+
https://nuxt.com/docs/api/utils/dollarfetch
46+
47+
## チャレンジ
48+
49+
1. API の動作を確かめてみる\
50+
server/api/todos/index.ts に 4 つめの Todo を追加した後、リフレッシュボタンを押してデータが更新されることを確認してみましょう。
51+
2. 型付の自動化を確認してみる\
52+
server/api/todos/index.ts の Todo に `completed` プロパティを追加し、useFetch の型が更新されていることを確認してみましょう。
53+
54+
:ButtonShowSolution{.bg-faded.px4.py2.mb3.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50}

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export default defineNuxtConfig({
1212
'@nuxtjs/seo',
1313
'nuxt-icon',
1414
'@nuxt/eslint',
15-
1615
// local
1716
'~/modules/template-loader',
1817
'~/modules/nuxt-link',
18+
'@nuxt/image',
1919
],
2020
colorMode: {
2121
classSuffix: '',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@nuxt/content": "^2.12.1",
4747
"@nuxt/devtools": "^1.3.1",
4848
"@nuxt/eslint": "^0.3.13",
49+
"@nuxt/image": "^1.7.0",
4950
"@nuxt/kit": "^3.11.2",
5051
"@nuxtjs/color-mode": "^3.4.1",
5152
"@nuxtjs/seo": "2.0.0-rc.10",

0 commit comments

Comments
 (0)