How to set a default value to input with a v-model in Vue

featured Image

In this article, we will learn how we can set a default value for an input field with v-model in vuejs.

Let’s suppose we have a form with some input fields like name, email, and amount.

<template>
    ...
    <input type="text" class="form-control" v-model="amount">
    ...
</template>

And we want to set a default value for the amount field in the form.

So, to set a default value for the amount field in the form, we can use the v-model’s two-way binding.

We have to set the value of the amount in the data object in the script tag like this.

data:  () => {
return {
  amount: 2000
 }
}

Now, when we load the form on our page, we can see the default value of the amount as 2000 in the form input field.

And if you want to change the default amount value to a different number, then you can change it in the input field of the form.

The amount value in the data object will be overwritten by the new value.

Related Topics:

Get value from input using on change in Vue

Related Posts

featured Image

How to add common header and footer in vuejs.

Here, in this article, we will learn how to add a common header and footer component in our vuejs application. In general, we always have the same header and footer…

Read more
featured Image

How to call a function in VUE template?

Here, we will learn on how to call a function in vue template. First, we have to define the function in the methods. and then we can use an event…

Read more
featured Image

How to redirect to external url in vue

Here, we will learn how to redirect to an external URL in vuejs application. Page Navigation is one of the basic features of a website. It helps us to navigate…

Read more