DEV Community

Cover image for How to Vue - Props vs Data

How to Vue - Props vs Data

Milos Protic on February 05, 2019

Vue is shipped with two ways of storing variables. One way is to use props, and the other way is to use data. Both of them can be defined on the Vu...
Collapse
 
zymawy profile image
Ironside

Wow, Thank You For Such Explanations.
I Like The Way You Made The Differences For Both data, props.

Collapse
 
proticm profile image
Milos Protic • Edited

Thank you, I appreciate it.

Collapse
 
akwetey profile image
Jonathan Akwetey

is it possible to do a two way binding by using v-model to get dynamic props values?
I mostly do that for data.

Collapse
 
proticm profile image
Milos Protic

If I understand the question correctly, I think that this should not be done in the first place.

Two way binding is exactly what its name says. A binding that goes in two directions. A property value comes from the parent data, so if you bind to it, since it is a two way binding, while changing the bound input value you are actually trying to mutate the parent data variable which, as said in this post is done with events ($emit option).

You are able to do this for data variables because, I assume, you have the data object defined on the component as a local state.

Collapse
 
akwetey profile image
Jonathan Akwetey

yeah you assumed correctly

Collapse
 
stikoo profile image
Alastair Hodgson

Yo Jonathan, v-modal is indeed used for two way binding, just not between parent-child components. You would use v-model to bind a components data to form inputs within the components

Collapse
 
marwicha profile image
marwicha • Edited

if you read the style guide of vue js is not recommended to define props type like this, it's better to do it like this for example:
props: {
propName: {
type: Object,
required: true,
},
propName: {
type: String,
required: true,
},
}

Collapse
 
e_paul_ profile image
eli

Nice Publication ever, make me join the community so i can thank you

Collapse
 
proticm profile image
Milos Protic

No problem, glad it helped you

Collapse
 
tarunvella profile image
tarunvella

An eye opener, Thanks a ton on explaining the subtle differences between data and props 🙌

Collapse
 
mehdiraash profile image
Mehdi Raash

It has to be misleading, having two things but doing actually one thing, I like polymer project approach which data & prop are one, but you can specify it by a property.

Collapse
 
abidemit profile image
Tiamiyu Sikiru Abidemi

Thanks for the break down, it's an eye opener for me, learnt some vital lessons.

Collapse
 
proticm profile image
Milos Protic

No problem, glad I could help