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

Commit 5ea7ce2

Browse files
authored
feat: composables
* feat(wip): Added to the document * feat(wip): add playground file * fix: change js to ts * fix: add attribute lang="ts" * fix: change meta file * fix: revert lang="ts" * fix: update * fix: js to ts * fix: remove generics * fix: add type="button" attribute
1 parent 9fff42a commit 5ea7ce2

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script setup lang="ts">
2+
const count = ref(1)
3+
const doubled = computed(() => count.value * 2)
4+
5+
function increment() {
6+
count.value++
7+
}
8+
</script>
9+
10+
<template>
11+
<div>
12+
<p>count is {{ count }}</p>
13+
<p>doubled is {{ doubled }}</p>
14+
<button type="button" @click="increment">
15+
+1
16+
</button>
17+
</div>
18+
</template>

content/1.vue/4.composition-api/.template/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const meta: GuideMeta = {
44
startingFile: 'app.vue',
55
features: {
66
terminal: false,
7-
fileTree: false,
7+
fileTree: true,
88
navigation: false,
99
},
1010
}
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, doubled, increment } = useCounter()
3+
</script>
4+
5+
<template>
6+
<div>
7+
<p>count is {{ count }}</p>
8+
<p>doubled is {{ doubled }}</p>
9+
<button type="button" @click="increment">
10+
+1
11+
</button>
12+
</div>
13+
</template>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function useCounter() {
2+
const count = ref(1)
3+
const doubled = computed(() => count.value * 2)
4+
5+
function increment() {
6+
count.value++
7+
}
8+
9+
return { count, doubled, increment }
10+
}
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1-
# Vue Composition API
1+
# Composables とは何か ?
22

3-
// TODO:
3+
[Composables](https://ja.vuejs.org/guide/reusability/composables.html)とは、VueのComposition APIを活用して再利用可能な状態やロジックを定義するための機能です。Options APIで主流な[mixins](https://ja.vuejs.org/api/options-composition.html#mixins)と類似したコンセプトですが、より柔軟で再利用性の高い機能を提供します。Composition APIの詳しい説明は[こちら](https://ja.vuejs.org/guide/extras/composition-api-faq.html)をご参照ください。
4+
5+
Composablesの主な特徴は以下の通りです。
6+
7+
- **再利用可能なロジック**: Composablesを使用すると、コンポーネント間で共有したいロジックや状態をモジュールとして定義し、それを簡単にインポートして使用できます。
8+
- **関数として定義**: Composablesは通常、関数として定義され、必要な状態やメソッドを返します。この関数はVueのComposition APIを使用して内部で状態管理や副作用を処理します。
9+
- **明示的な依存関係**: Composablesを使うことで、依存関係が明示的になり、どのロジックや状態がどのコンポーネントで使用されているかが明確になります。
10+
11+
Nuxtでは、`composables/`ディレクトリにComposablesなロジックを格納することが多く、[自動インポート](https://nuxt.com/docs/examples/features/auto-imports)の対象になります。
12+
13+
## チャレンジ問題
14+
15+
それでは、これらの特徴を踏まえて以下のステップでロジックをComposablesとして切り出し、再利用してみましょう。
16+
17+
1. 既存のvueファイル(`app.vue`)の確認してください。
18+
2. カウンターロジックをComposableとして切り出してください。具体的には`composables/`フォルダを作成し、その中に`useCounter.ts`というファイルを作成してください。
19+
3. `app.vue` に2で切り出したロジックを適用してください。
20+
21+
## ヒント
22+
23+
- Composableは関数として定義され、必要な状態やメソッドを返します。
24+
- `composables/`ディレクトリに作成したロジックは自動インポートが適用されます。
25+
26+
:ButtonShowSolution{.bg-faded.px4.py2.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50}

0 commit comments

Comments
 (0)