Vue.js Custom Component With v Model

May 3, 2021

Custom Input Component: CustomInput.vue using @input

<template>  <div>    <input :value="value" @input="$emit('input', $event.target.value)" />    <div>Local Value: {{ value }}</div>  </div></template><script>export default {  props: ['value'],  methods: {    updateValue(value) {      this.$emit('input', value)    }  }}  </script>

or using v-model

<template>  <div>    <b-form-input v-model="localValue"></b-form-input>    <div>Local Value: {{ value }} / {{ localValue }}</div>  </div></template><script>export default {  props: ['value'],  computed: {    localValue: {      get() { return this.value },      set(value) { this.$emit('input', value) }    }  }}  </script>

Usage

<template>  <custom-input v-model="value"></custom-input></template><script>export default {  data() {    return {      value: 'test'    }  }}</script>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.