-
Notifications
You must be signed in to change notification settings - Fork 87
Guide > Composition API > Setup の翻訳を追従 #283
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8337e02
Note that props can be accessed in template directly
naokie c365832
fix: fixed link to unwrapping; close
naokie bc2af46
Resolve props template usage composition api
naokie f88efaa
docs: clarify the usage of toRefs and toRef for optional props
naokie e46ee04
docs: shallow unwrap on setup
naokie f46949d
fix: missing anchor link
naokie d79696a
fix: follow spelling inconsistencies ruls #94
naokie 06875de
fix: translate 'render function'
naokie 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 hidden or 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
This file contains hidden or 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,12 +1,12 @@ | ||
# セットアップ | ||
|
||
> このセクションではコード例に[単一ファイルコンポーネント](single-file-component.html)のシンタックスを使います。 | ||
> このセクションではコード例に[単一ファイルコンポーネント](single-file-component.html)の構文を使います。 | ||
|
||
> このガイドは[コンポジション API の導入](composition-api-introduction.html)と[リアクティブの基礎](reactivity-fundamentals.html)を既に読んでいることを想定しています。 コンポジション API に初めて触れる方は、まずそちらを読んでみてください。 | ||
> このガイドは[Composition API の導入](composition-api-introduction.html)と[リアクティブの基礎](reactivity-fundamentals.html)を既に読んでいることを想定しています。 Composition API に初めて触れる方は、まずそちらを読んでみてください。 | ||
|
||
## 引数 | ||
|
||
`setup` 関数を使う時、2 つの引数を取ります: | ||
`setup` 関数を使う時、2つの引数を取ります: | ||
|
||
1. `props` | ||
2. `context` | ||
|
@@ -15,7 +15,7 @@ | |
|
||
### プロパティ | ||
|
||
`setup` 関数の 第一引数は `props` 引数です。 標準コンポーネントで期待するように、`setup` 関数内の `props` はリアクティブで、新しい props が渡されたら更新されます。 | ||
`setup` 関数の第1引数は `props` 引数です。 標準コンポーネントで期待するように、`setup` 関数内の `props` はリアクティブで、新しい props が渡されたら更新されます。 | ||
|
||
```js | ||
// MyBook.vue | ||
|
@@ -34,7 +34,7 @@ export default { | |
しかし、`props` はリアクティブなので、**ES6 の分割代入を使うことができません。** props のリアクティブを削除してしまうからです。 | ||
::: | ||
|
||
もし、props を分割代入する必要がある場合は、`setup` 関数内で [toRefs](reactivity-fundamentals.html#destructuring-reactive-state) を使うことによって安全に分割代入を行うことができます。 | ||
もし、props を分割代入する必要がある場合は、`setup` 関数内で [toRefs](reactivity-fundamentals.html#リアクティブな状態の分割代入) を使うことによって分割代入を行うことができます: | ||
|
||
```js | ||
// MyBook.vue | ||
|
@@ -48,9 +48,23 @@ setup(props) { | |
} | ||
``` | ||
|
||
`title` オプションのプロパティである場合、 `props` から抜けている可能性があります。その場合、 `toRefs` では `title` の ref はつくられません。代わりに `toRef` を使う必要があります: | ||
|
||
```js | ||
// MyBook.vue | ||
|
||
import { toRef } from 'vue' | ||
|
||
setup(props) { | ||
const title = toRef(props, 'title') | ||
|
||
console.log(title.value) | ||
} | ||
``` | ||
|
||
### コンテキスト | ||
|
||
`setup` 関数に渡される第二引数は `context` です。`context` は 3 つのコンポーネントプロパティを公開する一般的な JavaScript オブジェクトです。: | ||
`setup` 関数に渡される第2引数は `context` です。`context` は3つのコンポーネントプロパティを公開する一般的な JavaScript オブジェクトです。: | ||
|
||
```js | ||
// MyBook.vue | ||
|
@@ -69,7 +83,7 @@ export default { | |
} | ||
``` | ||
|
||
`context` オブジェクトは一般的な JavaScript オブジェクトです。 すなわち、リアクティブではありません。これは `context` で ES6 分割代入を安全に使用できることを意味します。 | ||
`context` オブジェクトは一般的な JavaScript オブジェクトです。すなわち、リアクティブではありません。これは `context` で ES6 分割代入を安全に使用できることを意味します。 | ||
|
||
```js | ||
// MyBook.vue | ||
|
@@ -84,34 +98,37 @@ export default { | |
|
||
## コンポーネントプロパティへのアクセス | ||
|
||
`setup` が実行されるとき、 コンポーネントインスタンスはまだ作成されていません。そのため、以下のプロパティにのみアクセスすることができます。: | ||
`setup` が実行されるとき、 コンポーネントインスタンスはまだ作成されていません。そのため、以下のプロパティにのみアクセスすることができます: | ||
|
||
- `props` | ||
- `attrs` | ||
- `slots` | ||
- `emit` | ||
|
||
言い換えると、以下のコンポーネントオプションには**アクセスできません**。: | ||
言い換えると、以下のコンポーネントオプションには**アクセスできません**: | ||
|
||
- `data` | ||
- `computed` | ||
- `methods` | ||
|
||
## テンプレートでの使用 | ||
|
||
`setup` がオブジェクトを返す場合、コンポーネントのテンプレート内でオブジェクトのプロパティにアクセスすることができます。: | ||
`setup` がオブジェクトを返す場合、コンポーネントのテンプレート内でオブジェクトのプロパティにアクセスすることができ、 `setup` に渡された `props` のプロパティも同じようにアクセスできます: | ||
|
||
```vue-html | ||
<!-- MyBook.vue --> | ||
<template> | ||
<div>{{ readersNumber }} {{ book.title }}</div> | ||
<div>{{ collectionName }}: {{ readersNumber }} {{ book.title }}</div> | ||
</template> | ||
|
||
<script> | ||
import { ref, reactive } from 'vue' | ||
|
||
export default { | ||
setup() { | ||
props: { | ||
collectionName: String | ||
}, | ||
setup(props) { | ||
const readersNumber = ref(0) | ||
const book = reactive({ title: 'Vue 3 Guide' }) | ||
|
||
|
@@ -125,11 +142,11 @@ export default { | |
</script> | ||
``` | ||
|
||
`setup` から返された [refs](../api/refs-api.html#ref) は、テンプレート内でアクセスされたときに[自動的にアンラップ](../api/refs-api.html#access-in-templates)されるので、テンプレート内で `.value` を使用すべきではないことに注意してください。 | ||
`setup` から返された [refs](../api/refs-api.html#ref) は、テンプレート内でアクセスされたときに[自動的に浅いアンラップ](/guide/reactivity-fundamentals.html#ref-のアンラップ)されるので、テンプレート内で `.value` を使用すべきではないことに注意してください。 | ||
|
||
## 描画関数での使用 | ||
## レンダリング関数での使用 | ||
|
||
`setup` は同じスコープで宣言されたリアクティブなステートを直接利用することができる描画関数を返すこともできます。: | ||
`setup` は同じスコープで宣言されたリアクティブなステートを直接利用することができるレンダリング関数を返すこともできます: | ||
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. ここのレンダリング関数も同じく |
||
|
||
```js | ||
// MyBook.vue | ||
|
This file contains hidden or 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
This file contains hidden or 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
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.
レンダリング関数
は、render という用語が一般的になってきたので、英語表記Render 関数
とにしておきたいです。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.
他のフレームワークの話をするときも、一般化してきましたね。
https://v3.ja.vuejs.org/guide/render-function.html
ドキュメント全体としても、ここが用語のソースになるからこれで統一ですね。