Skip to content

Commit bde755b

Browse files
mtmtkzmkazupon
andauthored
docs: #17 翻訳 "Components In-Depth > Provide / inject" (#79)
* docs: #17 translate "Components In-Depth > Provide / inject" * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> * Update src/guide/component-provide-inject.md Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com> Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com>
1 parent b51c5d8 commit bde755b

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/guide/component-provide-inject.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Provide / inject
22

3-
> This page assumes you've already read the [Components Basics](component-basics.md). Read that first if you are new to components.
3+
> このページは既に[コンポーネントの基本](component-basics.md)を読んでいる事を前提としています。 コンポーネントを初めて使う方はそちらを先にお読みください。
44
5-
Usually, when we need to pass data from the parent to child component, we use [props](component-props.md). Imagine the structure where you have some deeply nested components and you only need something from the parent component in the deep nested child. In this case, you still need to pass the prop down the whole component chain which might be annoying.
5+
通常、親コンポーネントから子コンポーネントにデータを渡すとき、[props](component-props.md) を使います。深くネストされたいくつかのコンポーネントがあり、深い階層にあるコンポーネントが浅い階層にあるコンポーネントの何かしらのデータのみを必要としている構造を想像してください。この場合でも、鎖のように繋ったコンポーネント全体にプロパティを渡す必要がありますが、時にそれは面倒になります。
66

7-
For such cases, we can use the `provide` and `inject` pair. Parent components can serve as dependency provider for all its children, regardless how deep the component hierarchy is. This feature works on two parts: parent component has a `provide` option to provide data and child component has an `inject` option to start using this data.
7+
そのような場合は、`provide``inject` のペアを利用できます。コンポーネント階層の深さに関係なく、親コンポーネントは、そのすべての子階層へ依存関係を提供するプロバイダとして機能することができます。この機能は2つの機能からなります:
8+
親コンポーネントは、データを提供するためのオプション `provide` を持ち、子コンポーネントはそのデータを利用するためのオプション `inject` を持っています。
89

910
![Provide/inject scheme](/images/components_provide.png)
1011

11-
For example, if we have a hierarchy like this:
12+
例えば、このような構造があるとします:
1213

1314
```
1415
Root
@@ -19,7 +20,7 @@ Root
1920
└─ TodoListStatistics
2021
```
2122

22-
If we want to pass the length of todo-items directly to `TodoListStatistics`, we would pass the prop down the hierarchy: `TodoList` -> `TodoListFooter` -> `TodoListStatistics`. With provide/inject approach, we can do this directly:
23+
もし todo-items のサイズを `TodoListStatistics` に渡したい場合、プロパティをこのように渡します: `TodoList` `TodoListFooter` `TodoListStatistics`provide/inject を利用すると、これを直接的に行えます。
2324

2425
```js
2526
const app = Vue.createApp({})
@@ -49,7 +50,7 @@ app.component('todo-list-statistics', {
4950
})
5051
```
5152

52-
However, this won't work if we try to provide some component instance property here:
53+
ただし、ここでコンポーネントのインスタンスプロパティを提供しようとしても、うまく動作しないでしょう:
5354

5455
```js
5556
app.component('todo-list', {
@@ -67,7 +68,7 @@ app.component('todo-list', {
6768
})
6869
```
6970

70-
To access component instance properties, we need to convert `provide` to be a function returning an object
71+
コンポーネントのインスタンスプロパティにアクセスするためには、`provide` をオブジェクトを返す関数へ変換する必要があります
7172

7273
```js
7374
app.component('todo-list', {
@@ -87,16 +88,16 @@ app.component('todo-list', {
8788
})
8889
```
8990

90-
This allows us to more safely keep developing that component, without fear that we might change/remove something that a child component is relying on. The interface between these components remains clearly defined, just as with props.
91+
こうすることで、子コンポーネントが依存している何かを変更したり削除したりしてしまうことを恐れることなく、より安全にコンポーネントを開発し続けることができます。これらのコンポーネント間のインターフェースは、props と同じく、明確に定義されています。
9192

92-
In fact, you can think of dependency injection as sort of “long-range props”, except:
93+
実際、依存関係の注入は、いわば「長距離な props 」のように考えることができます。後述の点を除きます:
9394

94-
- parent components don’t need to know which descendants use the properties it provides
95-
- child components don’t need to know where injected properties are coming from
95+
- 親コンポーネントは、提供したプロパティを子孫コンポーネントのどこで使用しているか知る必要がありません
96+
- 子コンポーネントは、注入されたプロパティがどこから提供されたものなのか知る必要がありません
9697

97-
## Working with reactivity
98+
## リアクティブと連携する
9899

99-
In the example above, if we change the list of `todos`, this change won't be reflected in the injected `todoLength` property. This is because `provide/inject` bindings are _not_ reactive by default. We can change this behavior by passing a `ref` property or `reactive` object to `provide`. In our case, if we wanted to react to changes in the ancestor component, we would need to assign a Composition API `computed` property to our provided `todoLength`:
100+
前述の例では、リスト `todos` を変更しても、その変更は注入された `todoLength` には反映されません。これは、`provide/inject` の束縛( binding )がデフォルトでリアクティブ _でない_ ことが原因です。`ref` で定義されたプロパティや `reactive` で作成されたオブジェクトを `provide` に渡すことにより、この振る舞いを変更することができます。この場合、祖先コンポーネントをリアクティブにするためには、コンポジション API `computed` で定義したプロパティを `todoLength` を割り当てる必要があります。
100101

101102
```js
102103
app.component('todo-list', {
@@ -109,4 +110,4 @@ app.component('todo-list', {
109110
})
110111
```
111112

112-
In this, any change to `todos.length` will be reflected correctly in the components, where `todoLength` is injected. Read more about `reactive` provide/inject in the [Composition API section](composition-api-provide-inject.html#injection-reactivity)
113+
こうすると、`todos.length`へのあらゆる変更が、`todoLength` が注入されたコンポーネントに正しく反映されます。`reactive` provide/inject の詳細については、[コンポジション API セクション](composition-api-provide-inject.html#injection-reactivity) をご覧ください。

0 commit comments

Comments
 (0)