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
propsDatain the parent component or component?– ThiagoO
What are the
fasesthat each one is used?– ThiagoO
@Thiagoluizs the
propsDatais used to create the component you havepropsclosed. You can create a component with a function, it does not need to be a parent component. There are no "phases", thepropsDatais 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
propshas priority overpropsData?– ThiagoO
@Thiagoluizs
propsDataandpropsis the same thing. When the component is started it consumes thepropsDatathat the object passed to the constructor receives and the instance makes this object available inprops.– Sergio