1

I have a Vue component that contains a form that will send an email.

I am having an issue with displaying a default value in the input field. When the page loads I want the email input field to display the user's default email address and only change the value if the user over-writes it with a new email.

I know you can't have v-bind and v-model on the same <input> so how would I go about accomplishing this task?

<input
  v-model="emailAddress"
  :value="emailAddress"
  type="email"
  name="email"
  id="email-field"
/>
export default {
  props: { defaultEmail: String },
  data() {
    return {
      emailAddress: this.defaultEmail || ''
    };
  }
}

2 Answers 2

3

You only need v-model to bind the input value:

const emailComponent = Vue.component('emailComponent', { 
  template: '#emailComponent',
  data () {
    return {
      emailAddress: this.defaultEmail || ''
    }
  },
  props: { defaultEmail: String },
});

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

<div id="app">
  <email-component default-email="my email"/>
</div>

<template id="emailComponent">
  <input
    v-model="emailAddress"
    type="email"
    name="email"
    id="email-field"
  />
</template>

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

Comments

1

v-model does the same thing as

<input
  v-bind:value="searchText"
  v-on:input="searchText = $event.target.value"
>

So you do not need value binding, you can remove it just leave v-model. Check vue doc for more info: https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components

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.