Using v-model on custom component
Asked Answered
C

3

22

When I start typing in the input field I want to get this data in console, but currently it is empty. What am I doing wrong?

HTML:

<products-list v-model="product.name" v-on:keyup="productName"></products-list>

JS:

Vue.component('products-list', {
    template:
        `<input class="product_name form-control" contenteditable="true"></input>`,
});

var app = new Vue({
    el: '#app',
    data: {
        items: items,
        product: {
            name: "",
        }
    },
    methods: {
        productName: function() {
            console.log(product.name);
        }
    }
});
Cairo answered 16/9, 2017 at 21:54 Comment(0)
B
33

v-model uses the @input event by default, so if you want to use v-model on a custom component you need to emit the input event to the parent. So, in your component, you simply do:

<input class="product_name form-control" @input="$emit('input', $event.target.value)" />

Now in your parent you can do:

<products-list v-model="product.name"></products-list>

You can see the full example on this JSFiddle: https://jsfiddle.net/7s2ugt11/

Bobble answered 17/9, 2017 at 9:56 Comment(0)
A
35

There are breaking changes for Vue 3.x:

BREAKING: When used on custom components, v-model prop and event default names are changed:
prop: value -> modelValue
event: input -> update:modelValue

https://v3-migration.vuejs.org/breaking-changes/v-model.html

So your child component would be:

<template>
  <input :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" />
</template>

<script>
export default {
  name: "ProductsList",
  props: ['modelValue']
}
</script>

And in the parent component you would not change anything:

<products-list v-model="product.name"></products-list>
Agonize answered 28/10, 2020 at 11:22 Comment(0)
B
33

v-model uses the @input event by default, so if you want to use v-model on a custom component you need to emit the input event to the parent. So, in your component, you simply do:

<input class="product_name form-control" @input="$emit('input', $event.target.value)" />

Now in your parent you can do:

<products-list v-model="product.name"></products-list>

You can see the full example on this JSFiddle: https://jsfiddle.net/7s2ugt11/

Bobble answered 17/9, 2017 at 9:56 Comment(0)
S
8

Using v-model in custom components sometimes will lead us to some conflicts.

Or we just want to use the value for different purpose.

That's why vue introduced model. Please take a look to it.

<!-- Parent Component -->
<div id="app">
  <my-checkbox v-model="checked" value="unchanged"></my-checkbox>{{checked}}
</div>

<!-- Child Component -->
<template id="my_checkbox">
  <div>
   My value is {{value}} <br>
   <input type="checkbox" :value="value" @change="$emit('change', !checked)">  
  </div>
</template>

And the script:

Vue.component('my-checkbox', {
    template: '#my_checkbox',
  model: {
    prop: 'checked',
    event: 'change'
  },
  props: {
    // this allows using the `value` prop for a different purpose
    value: String,
    // use `checked` as the prop which take the place of `value`
    checked: {
      type: Boolean,
      default: false
    }
  },
})

new Vue({
  el: "#app",
  data: {
        checked: null
  },    
})

See it in action

Sholokhov answered 9/6, 2018 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.