Data binding is a process in which a data source (such as a model or a component state) is linked to a view (such as a template or a component) in such a way that changes to the data source are automatically reflected in the view.
It is a way to connect data from your program to the things you see on the screen. Think of it like a bridge between your data and what you see.
For example, let’s say you’re playing a video game and you have a character with a certain number of lives. In the game, you can see the number of lives your character has left on the screen. This is an example of data binding. The number of lives is the data and what you see on the screen is the view.
With Vue.js, data binding is used to make sure that when the data changes, what you see on the screen changes too. In Vue.js, data binding is achieved using a template syntax that integrates with the Vue.js reactivity system. For example, to bind a data property message to a template, you can use double curly braces {{ }} like this:
<template>
<div> {{ message }}</div>
</template>
<script>
export default{
data(){
return {
message: 'Hello, Vue.js!'
};
}
};
</script>
In this example, when the value of the message
property changes, the view will automatically update to reflect the change. This makes it easy to create dynamic and reactive applications with Vue.js. When you run this code, you will see “Hello, World!” displayed on the screen. If you change the value of message
to something else, like “Hi there!”, the view will automatically update to show the new message. That’s data binding!
How to Bind Input Data in Vue.js
In Vue.js, you can bind input data to a component’s data property using v-model
directive. This directive creates a two-way binding between the input field and the data property, so that changes made to the input field are automatically reflected in the data property, and vice versa.
Here’s an example of using v-model
directive with a text input field:
<template>
<div>
<input type="text" v-model="message">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, World!'
};
}
};
</script>
In this example, the v-model directive binds the input field to the message
data property. When you type in the input field, the value of the message
property will automatically update, and the view will update to show the new value of message
.
Here’s an example of using v-model
directive with checkbox input:
<template>
<div>
<input type="checkbox" v-model="isChecked">
<p>{{ isChecked }}</p>
</div>
</template>
<script>
export default {
data() {
return {
isChecked: false
};
}
};
</script>
In this example, the v-model directive binds the checkbox input to the isChecked
data property. When you check or uncheck the checkbox, the value of the isChecked
property will automatically update, and the view will update to show the new value of isChecked
.
You can also use v-model
directive with radio buttons and select elements in a similar way.
If you do enjoy reading this please share it with others and give it a like.