How to set a default value to input with a v-model in Vue
Find out the simple way on how to set a default value to input with v-model in vue.
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:
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
