6
In the Vue.js
we have how to insert data into components through props
as follows.
const hello = {
template: '<div>{{ message }}',
props: ['message']
};
new Vue({
el: '#app',
data: {
message: 'Pt Stackoverflow',
},
components: {
hello,
}
});
Reading the documentation further I came across propsData
that according to the documentation:
Passes props to an instance during its creation. The intention of this is primarily make unit testing easier.
I’m new to the Vue.js
so I’m not an expert in component testing, where according to the text above would be an example of using this strategy case.
Could someone explain me in a way that I understand the difference between the two and if possible an example of the use of propsData
?
Where I define the
propsData
in the parent component or component?– ThiagoO
What are the
fases
that each one is used?– ThiagoO
@Thiagoluizs the
propsData
is used to create the component you haveprops
closed. You can create a component with a function, it does not need to be a parent component. There are no "phases", thepropsData
is the object that will be used by the new component in itsprops
.– Sergio
Thank you I understand now.
– ThiagoO
My last doubt in the documentation is said that it is used primarily in tests and only during the creation phase of the instance, is it a way to inject data to perform tests? And after the creation
props
has priority overpropsData
?– ThiagoO
@Thiagoluizs
propsData
andprops
is the same thing. When the component is started it consumes thepropsData
that the object passed to the constructor receives and the instance makes this object available inprops
.– Sergio