0

I created a custom input

<template>
    <div class="content">
        <p>
            <slot></slot>
        </p>
        <input v-model="content" class="textbox" :type="type" @input="handleInput">
    </div>
</template>

<script>
export default {
  name: 'vTextbox',
  props:{
      type: String,
  }, 
  data: function(){
      return {
          content: ""
      }
  },
  methods:{
        handleInput(){
          this.$emit('input', this.content)
      }
  }
}
</script>

Parent component call the custom input component to grab its content such as:

<vTextbox v-model="email" type="email">Email</vTextbox>
export default {
  ...
  data: function(){
      return{
          email: "",
      }
  },
  methods:{
    Clear: function(){
        this.email = ""
    }
  }
}

I want to clear value/content of my custom input component when Clear function is called. I try to set this.email="" but it does not work.

1 Answer 1

1

the issue is that you're not receiving the value in the custom input. While you have v-model in the parent component, for the v-model magic to work the component needs to implement the value prop and watch for change.

Here is what that might look like

<template>
    <div class="content">
        <p>
            <slot></slot>
        </p>
        <input v-model="content" class="textbox" :type="type" @input="handleInput">
    </div>
</template>

<script>
export default {
  name: 'vTextbox',
  props:{
      value: String, // added value prop
      type: String,
  }, 
  data: function(){
      return {
          content: ""
      }
  },
  watch:{
    value(val) {
      this.content = val; // added watch to override internal value, this will allow clear to work
    }
  },
  methods:{
        handleInput(){
          this.$emit('input', this.content)
      }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

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.