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

Commit 55a60c5

Browse files
authored
feat: 2.concepts/4.auto-imports (#34)
* feat: 2.concepts/4.auto-imports * feat: content/2.concepts/4.auto-imports
1 parent 22b579b commit 55a60c5

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script setup lang="ts">
2+
const count = ref(0)
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="count++">
7+
count is: {{ count }}
8+
</button>
9+
10+
<Counter />
11+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
const { count, increment } = useCounter()
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="increment">
7+
count is: {{ count }}
8+
</button>
9+
</template>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function useCounter() {
2+
const count = ref(0)
3+
function increment() {
4+
count.value++
5+
}
6+
7+
return { count, increment }
8+
}
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: 'pages/index.vue',
5+
features: {
6+
fileTree: true,
7+
},
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
const count = ref(0)
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="count++">
7+
count is: {{ count }}
8+
</button>
9+
10+
<p>double is: {{ double(count) }}</p>
11+
12+
<Counter />
13+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
const { count, increment } = useCounter()
3+
</script>
4+
5+
<template>
6+
<button type="button" @click="increment">
7+
count is: {{ count }}
8+
</button>
9+
</template>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function useCounter() {
2+
const count = ref(0)
3+
function increment() {
4+
count.value++
5+
}
6+
7+
return { count, increment }
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function double(n: number): number {
2+
return n * 2
3+
}

content/2.concepts/4.auto-imports/index.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,49 @@ ogImage: true
33
---
44

55
# Auto Imports
6+
7+
Auto Imports も Nuxt のコアコンセプトの一つです。
8+
9+
https://nuxt.com/docs/guide/concepts/auto-imports
10+
11+
Auto Imports は明示的にインポートすることなく、コンポーネント、コンポーザブル、および [Vue.js の API](https://vuejs.org/api/) をアプリケーション全体で使用できるように自動的にインポートする機能です。\
12+
従来のグローバル宣言とは異なり、Nuxt は型情報や IDE の補完、ヒントを保持し、本番コードで使用されているもののみを含めます。
13+
14+
Nuxt のディレクトリ構造の規約おかげで、 `components/``composables/`、および `utils/` を自動的にインポートすることができます。\
15+
この例では、`components` ディレクトリに定義された Counter.vue コンポーネントと、`composables` ディレクトリに定義された `useCounter.ts` を明示的なインポートなしで使用しています。\
16+
`app.vue` では Counter コンポーネントを使用し、`Counter.vue` では `useCounter()` を使用しています。
17+
18+
また、Nuxt はいくつかのコンポーネントやコンポーザブル、ユーティリティも提供しています。
19+
[ルーティング](/concepts/routing) のセクションで登場した `NuxtLink` コンポーネントがその一例です。\
20+
他にも、データフェッチで利用する `useFetch()` コンポーザブルやランタイムの設定にアクセスる `useRuntimeConfig()` コンポーザブル、ページナビゲーションのための `navigateTo()` ユーティリティ関数などがあります。\
21+
たくさんあるので、そのほかのものは Nuxt 公式ドキュメントの [Components](https://nuxt.com/docs/api/components)[Composables](https://nuxt.com/docs/api/composables)[Utils](https://nuxt.com/docs/api/utils) のセクションを参照してください。
22+
23+
また、Nuxt では明示的なインポートもサポートしており、この場合は `#import` からインポートすることが可能です。
24+
25+
```ts
26+
import { computed, ref } from '#imports'
27+
28+
const count = ref(1)
29+
const double = computed(() => count.value * 2)
30+
```
31+
32+
Auto Imports 機能は `nuxt.config.ts` でオプトアウトすることも可能です。\
33+
この場合は上記の明示的なインポートが必要になります。
34+
35+
```ts
36+
// nuxt.config.ts
37+
export default defineNuxtConfig({
38+
imports: {
39+
autoImport: false
40+
}
41+
})
42+
```
43+
44+
## チャレンジ
45+
46+
実際に utils ディレクトリを作成して、Auto Import 可能な関数を実装してみましょう。
47+
48+
関数は任意のもので構いませんが、例として 「与えられた数値を二倍にして返す `double()` 関数」を実装してみましょう。\
49+
関数が実装できたら、`app.vue` 内の template で使用して、2倍された数値を画面に表示してみましょう。
50+
51+
:ButtonShowSolution{.bg-faded.px4.py2.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50}

0 commit comments

Comments
 (0)