Data can be provided from one component to another using Vue’s Provide/Inject function, especially in larger projects.
Provide allows other components to access data.
To retrieve the given data, utilize inject.
As an alternative to sharing data using props, you can share data by utilizing the Provide/Inject method.
It can be challenging to use props to transfer data from “App.vue” to a sub-component in a large project with components inside components since each component the data passes through needs to have its props set.
The provided data must only be defined where it is provided, and the injected data must only be defined where it is injected, if provide/inject is used in place of props.
In order to make data accessible to other components, we utilize the ‘supply’ configuration option:
App.vue:
Food
About
Kinds
The ‘foods’ array is now provided in the code above so that it may be injected into other project components.
We can now include the ‘foods’ array in the ‘FoodKinds’ component since ‘provide’ in ‘App.vue’ has made it available.
We can utilize the data from App.vue to display various foods in the ‘FoodKinds’ component after injecting the ‘foods’ data into it:
FoodKinds.vue:
Different Kinds of Food
In this application, food data is provided in "App.vue", and injected in the "FoodKinds.vue" component so that it can be shown here:
{{ x.name }}
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.