3

I'm loading a Vue component which should render a basic form. I'm trying to set the input to have a default value, but for some reason nothing shows. Here is what i tried:

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

Why does nothing show into the input field? Is it because Vue doesn't support it or i need to use something else? Thanks in advance

2 Answers 2

7

The reason why you don't have a default value, is that this value is overwritten by the

v-model = "amount"

In order to set a default value, it would be better to set the value of amount in the returned vue data object. You will have something like :

data:  () => {
return {
  amount: 0.02 // Your default value
 }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. I needed to set it from inside return {}. Thanks a lot
2

The default value is set in data as follows:

new Vue({
  el:"#app",
  data() { return { amount: "0.02" } }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <input type="text" class="form-control" v-model="amount">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.