I'm learning Vue with an online course and the instructor gave me an exercise to make an input text with a default value. I completed it using v-model
but, the instructor chose v-bind:value
and I don't understand why.
Can someone give me a simple explanation about the difference between these two and when it's better use each one?
The v-bind directive is a Vuejs directive used to bind one or more attributes, or a component prop to an element. If that attribute is binded to our data defined in Vuejs instance then dynamically changes can be observed as data changes.
Vue v-model is a directive that creates a two-way data binding between a value in our template and a value in our data properties. A common use case for using v-model is when designing forms and inputs. We can use it to have our DOM input elements be able to modify the data in our Vue instance.
The v-model directive makes two-way binding between a form input and app state very easy to implement. One can bind a form input element and make it change the Vue data property when the content of the field changes.
The V-model is an SDLC model where execution of processes happens in a sequential manner in a V-shape. It is also known as Verification and Validation model. The V-Model is an extension of the waterfall model and is based on the association of a testing phase for each corresponding development stage.
From here - Remember:
<input v-model="something">
is essentially the same as:
<input v-bind:value="something" v-on:input="something = $event.target.value" >
or (shorthand syntax):
<input :value="something" @input="something = $event.target.value" >
So v-model
is a two-way binding for form inputs. It combines v-bind
, which brings a js value into the markup and v-on:input
to update the js value. The js value must be present in your data
, or in an inject
.
Use v-model
when you can. Use v-bind
/v-on
when you must :-) I hope your answer was accepted.
v-model
works with all the basic HTML input types (text, textarea, number, radio, checkbox, select). You can use v-model
with input type=date
if your model stores dates as ISO strings (yyyy-mm-dd). If you want to use date objects in your model (a good idea as soon as you're going to manipulate or format them), do this.
v-model
has some extra smarts that it's good to be aware of. If you're using an IME ( lots of mobile keyboards, or Chinese/Japanese/Korean ), v-model will not update until a word is complete (a space is entered or the user leaves the field). v-input
will fire much more frequently.
v-model
also has modifiers .lazy
, .trim
, .number
, covered in the doc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With