Skip to content

Commit f88efaa

Browse files
committed
docs: clarify the usage of toRefs and toRef for optional props
vuejs/docs@a502ebe#diff-861c883c19deb5fffdbcbaf69ecc06bab99f7f21c623d3ea197fa227826b5c05
1 parent bc2af46 commit f88efaa

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/guide/composition-api-setup.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
しかし、`props` はリアクティブなので、**ES6 の分割代入を使うことができません。** props のリアクティブを削除してしまうからです。
3535
:::
3636

37-
もし、props を分割代入する必要がある場合は、`setup` 関数内で [toRefs](reactivity-fundamentals.html#destructuring-reactive-state) を使うことによって安全に分割代入を行うことができます。
37+
もし、props を分割代入する必要がある場合は、`setup` 関数内で [toRefs](reactivity-fundamentals.html#destructuring-reactive-state) を使うことによって分割代入を行うことができます:
3838

3939
```js
4040
// MyBook.vue
@@ -48,6 +48,20 @@ setup(props) {
4848
}
4949
```
5050

51+
`title` オプションのプロパティである場合、 `props` から抜けている可能性があります。その場合、 `toRefs` では `title` の ref はつくられません。代わりに `toRef` を使う必要があります:
52+
53+
```js
54+
// MyBook.vue
55+
56+
import { toRef } from 'vue'
57+
58+
setup(props) {
59+
const title = toRef(props, 'title')
60+
61+
console.log(title.value)
62+
}
63+
```
64+
5165
### コンテキスト
5266

5367
`setup` 関数に渡される第二引数は `context` です。`context` は 3 つのコンポーネントプロパティを公開する一般的な JavaScript オブジェクトです。:

0 commit comments

Comments
 (0)