Languages

Vue.js - what is v-model?

0 points
Asked by:
Mariam-Barron
841

What is v-model in Vue.js?

For example, I have a code similar to this:

<template>
  <div>
    <input type="text" v-model="username" />
    <input type="password" v-model="password" />
  </div>
</template>

and I don't really understand what does v-model change in the input element. 

1 answer
0 points
Answered by:
Mariam-Barron
841

The v-model is a two-way data binding directive which developers use to construct a binding between a form input element and a data property. It's a shorthand syntax for binding data to form elements and updating the data property when the user interacts with the form element.

Practical example

Let's say, you have an input element and a data property called "username" like in your code.

You can use v-model to bind the value of the input element to the "username" property. The "username" property's value will be automatically updated each time the user enters text into the input field.

app.vue file:

<template>
  <div>
    <input type="text" v-model="username" />
    <p>Username input value: {{ username }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const username = ref('');
</script>

Result:

Vue.js - v-model explanation result
Vue.js - v-model explanation result
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join