v-model
and v-bind
are both Vue.js directives, but they serve different purposes:
v-model
The v-model
directive is primarily used for two-way data binding on form inputs.
It creates a binding between a form input element and a piece of data in the Vue instance.
Changes made to the input value will automatically update the associated data property, and changes to the data property will update the input value.
For example we write
<input v-model="message" type="text">
In this example, message
is a data property in the Vue instance, and its value will be synchronized with the value of the input field.
v-bind
The v-bind
directive is used to bind an attribute or a component prop to an expression.
It’s typically used when we want to dynamically bind an attribute or a prop to a value that can change over time. Unlike v-model
, v-bind
doesn’t establish any two-way data binding.
For instance we write
<div v-bind:class="{ active: isActive }"></div>
In this example, the class
attribute of the <div>
element will be bound to the value of the isActive
data property.
If isActive
is true
, the active
class will be applied; otherwise, it will not.
In conclusion, v-model
is specifically for form inputs and creates a two-way data binding between the input value and a data property.
v-bind
is used to dynamically bind attributes or props to expressions and doesn’t create any two-way data binding. It’s more general-purpose and can be used with any HTML attribute or Vue component prop.