You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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:
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.
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`:
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