This repository was archived by the owner on May 1, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +67
-3
lines changed
content/1.vue/4.composition-api Expand file tree Collapse file tree 5 files changed +67
-3
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ export const meta: GuideMeta = {
4
4
startingFile : 'app.vue' ,
5
5
features : {
6
6
terminal : false ,
7
- fileTree : false ,
7
+ fileTree : true ,
8
8
navigation : false ,
9
9
} ,
10
10
}
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- # Vue Composition API
1
+ # Composables とは何か ?
2
2
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 }
You can’t perform that action at this time.
0 commit comments