Zego Tech
Zego Tech

Reputation: 21

Provide is used to pass values to child components, so how does the parent component that sends the provide get the data in the provide itself?

In Vue, provide can pass values to child components

provide('data',ref("I am data for child components"))

Subcomponents can use inject to get the value.

How to get the data in the parent component? ?

Upvotes: 0

Views: 30

Answers (1)

Kan Robert
Kan Robert

Reputation: 299

Normal pass by value:

let data = ref("I am data for child components");
provide('data',data);//This way the parent component can get 

it and the child component can get it too

Transfer Function:

let children = ref('')
function data(val){

children.value = val;//Get the value passed by the child component
}
provide('data',data);

Sub: let getData = inject('data',data) data is a function, you can getData('data for father')

Upvotes: 0

Related Questions