0

I am able to build a simple textbox component from <input /> and setup v-model binding correctly.

I'm trying to do same with a custom component: vs-input from vuesax.

Following the pattern below does not work as expected:

<template>
  <div>
    <vs-input type="text" v-model="value" @input="text_changed($event)" />
    <!-- <input type="text" :value="value" @input="$emit('input', $event.target.value)" /> -->
  </div>
</template>

<script>
export default {
  name: 'TestField',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {}
  },
  methods: {
    text_changed(val) {
      console.log(val)
      // this.$emit('input', val)
    }
  }
}
</script>

In building custom components from other custom components is there anything particular we should look out for to get v-model binding working properly?

Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157

2 Answers2

1

Following code might help you.(Sample code try it in codepen)

updating props inside a child component

//html
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>
  <input type="text" :value="test" @change="abc">
  {{ test }}
</div>

//VUE CODE

new Vue({
   el: '#app',
   data: {
     message: 'Hello Vue.js!',
      },
   props:{
     test:{
        type:String,
        default:''
     } 
   },
  methods:{
     abc:function(event){
      //console.log("abc");
      console.log(event.target.value);
      this.test=event.target.value;
    }
   }
  })
Pallamolla Sai
  • 2,337
  • 1
  • 13
  • 14
1

I prefer to interface props with computed:

<template>
  <div>
    <vs-input type="text" v-model="cValue" />
  </div>
</template>

<script>
export default {
  name: 'TestField',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {}
  },
  computed: {
    cValue: {
      get: function(){
        return this.value;
      },
      set: function(val){

         // do w/e
         this.$emit('input', val)
      }
    }
  }
}
</script>

Computed Setter

tony19
  • 125,647
  • 18
  • 229
  • 307
Estradiaz
  • 3,483
  • 1
  • 11
  • 22