-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: composablesセクションのハンズオン資料作成 #16
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d919619
feat(wip): Added to the document
shiyuu33 3eeb27d
feat(wip): add playground file
shiyuu33 9e9e759
Merge branch 'main' of https://github.com/vuejs-jp/learn.nuxt.com int…
shiyuu33 453d882
fix: change js to ts
shiyuu33 e8e2f15
fix: add attribute lang="ts"
shiyuu33 c9c6956
fix: change meta file
shiyuu33 1729946
fix: revert lang="ts"
shiyuu33 6724fba
fix: update
shiyuu33 91ce80f
fix: js to ts
shiyuu33 325805b
fix: remove generics
shiyuu33 603a978
fix: add type="button" attribute
shiyuu33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script setup lang="ts"> | ||
const count = ref<number>(1) | ||
const doubled = computed(() => count.value * 2) | ||
|
||
function increment() { | ||
count.value++ | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>count is {{ count }}</p> | ||
<p>doubled is {{ doubled }}</p> | ||
<button @click="increment"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
+1 | ||
</button> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
content/1.vue/4.composition-api/.template/solutions/app.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="ts"> | ||
const { count, doubled, increment } = useCounter() | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>count is {{ count }}</p> | ||
<p>doubled is {{ doubled }}</p> | ||
<button @click="increment"> | ||
+1 | ||
</button> | ||
</div> | ||
</template> |
10 changes: 10 additions & 0 deletions
10
content/1.vue/4.composition-api/.template/solutions/composables/useCounter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function useCounter() { | ||
const count = ref<number>(1) | ||
const doubled = computed(() => count.value * 2) | ||
|
||
function increment() { | ||
count.value++ | ||
} | ||
|
||
return { count, doubled, increment } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
# Vue Composition API | ||
# Composables とは何か ? | ||
|
||
// TODO: | ||
[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)をご参照ください。 | ||
|
||
Composablesの主な特徴は以下の通りです。 | ||
|
||
- **再利用可能なロジック**: Composablesを使用すると、コンポーネント間で共有したいロジックや状態をモジュールとして定義し、それを簡単にインポートして使用できます。 | ||
- **関数として定義**: Composablesは通常、関数として定義され、必要な状態やメソッドを返します。この関数はVueのComposition APIを使用して内部で状態管理や副作用を処理します。 | ||
- **明示的な依存関係**: Composablesを使うことで、依存関係が明示的になり、どのロジックや状態がどのコンポーネントで使用されているかが明確になります。 | ||
|
||
Nuxtでは、`composables/`ディレクトリにComposablesなロジックを格納することが多く、[自動インポート](https://nuxt.com/docs/examples/features/auto-imports)の対象になります。 | ||
|
||
## チャレンジ問題 | ||
|
||
それでは、これらの特徴を踏まえて以下のステップでロジックをComposablesとして切り出し、再利用してみましょう。 | ||
|
||
1. 既存のvueファイル(`app.vue`)の確認してください。 | ||
2. カウンターロジックをComposableとして切り出してください。具体的には`composables/`フォルダを作成し、その中に`useCounter.ts`というファイルを作成してください。 | ||
3. `app.vue` に2で切り出したロジックを適用してください。 | ||
|
||
## ヒント | ||
|
||
- Composableは関数として定義され、必要な状態やメソッドを返します。 | ||
- `composables/`ディレクトリに作成したロジックは自動インポートが適用されます。 | ||
|
||
:ButtonShowSolution{.bg-faded.px4.py2.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ジェネリクス渡さなくていいと思います
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ubugeeei
外しました!
325805b