In Vue.js, models are used to bind data to form inputs and update the data when the input changes. This allows for two-way data binding between the view and the model.
Here’s an example of how to use a model in Vue.js:
<template>
<div>
<input v-model="message" type="text">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: ''
}
}
}
</script>
In this example, we have an input element with the v-model
directive bound to the message
data property. This means that any changes to the input value will automatically update the message
property, and vice versa. The {{ message }}
expression is used to display the current value of the message
property in a paragraph element.
So, when the user types something into the input field, the message
property is updated, and the paragraph element is updated to display the new value of message
. This is all thanks to the two-way data binding provided by the v-model
directive.
