Using Vue v-model in child components with defineModel function

Posted by






Vue v-model in Child Components

Vue v-model in Child Components

Vue.js is a popular JavaScript framework for building user interfaces and single-page applications. It provides several powerful features that make it easy to build interactive and dynamic web applications. One of these features is the v-model directive, which allows two-way data binding between form input elements and component data.

When working with Vue components, you may need to pass data from a parent component to a child component and vice versa. This is where the v-model directive comes in handy. It allows you to bind a form input element to a piece of data and automatically sync the value of the input with the data. This makes it easy to create form elements that can be controlled from the parent component and child component.

Let’s take a look at an example of how to use v-model in a child component:

      <template>
        <div>
          <input v-model="childValue" />
        </div>
      </template>

      <script>
        export default {
          props: {
            modelValue: String
          },
          data() {
            return {
              childValue: this.modelValue
            }
          },
          watch: {
            modelValue(newValue) {
              this.childValue = newValue;
            },
            childValue(newValue) {
              this.$emit('update:modelValue', newValue);
            }
          }
        }
      </script>
    

In the example above, we have a child component that accepts a prop called “modelValue” and uses it as the initial value for the input element. We then use the v-model directive to bind the input element to the “childValue” data property. We also use a watcher to update the “childValue” when the “modelValue” prop changes and emit an event to update the “modelValue” when the “childValue” changes.

Another useful feature that Vue provides for working with v-model in child components is defineModel. This function allows you to define the model options for a child component, such as the prop name and event name to use for v-model. Here’s an example of how to use defineModel:

      <script>
        import { defineComponent, defineModel } from 'vue';

        export default defineComponent({
          setup(props, { emit }) {
            return defineModel({
              prop: 'modelValue',
              event: 'update:modelValue'
            });
          }
        });
      </script>
    

With defineModel, you can specify the name of the prop to use for the v-model value and the event name to use for updating the v-model value. This makes it easy to customize the v-model behavior in child components and maintain consistency throughout your application.

In summary, Vue v-model is a powerful feature that allows you to create form input elements that are easily controlled from parent components and child components. By using v-model and defineModel, you can build flexible and reusable form components in your Vue applications.


0 0 votes
Article Rating
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Diego Salas
7 months ago

This video was very helpful, thanks

Mikey WOrk
7 months ago

Does this work if the modelValue is an object that contains arrays?

Eduardo Donato
7 months ago

Hi, what vscode theme are you using?

Ulugbek Atakhanov
7 months ago

the best explanation. Great.

ניב אלישע
7 months ago

So glad I found this video.
I just started working with Vue after React and this topic was very confusing for me. Especially since the same thing can be done in different ways. Thanks!

CodeDjango
7 months ago

Thanks….followed your tutorial n works very well, however, in Vue DevTools, in the child component, it shows:

event-listeners:
update:model-value: not-declared

Is it that Vue devtools is not ready for this "defineModel" macro? Please suggest

Skia
7 months ago

If you try to console log the name ref in the app component, the value will not update in the console when the input value changes.

Can someone explain to me why? I am a React developer
How can i make the name ref update in the script tag?

Thanks in advance

Rafael Menezes
7 months ago

Nice content, Andre.

Aissa BOUGUERN
7 months ago

Great and informative video as usual!

Keno Frianeza
7 months ago

Thanks for the great video! Looking forward to more 🙂

munandi sichali
7 months ago

Any full courses that will come from you?

Johnny Driesen
7 months ago

Excellent Vid. Very well explained.
Thanks for willing to share your knowledge, and taking your time to create this vid.
Question to you.. R U a fan of TS ? And if I can ask … Why (not) ?

Oketa Fred
7 months ago

Awesome content Andre

Max Ralph
7 months ago

Wow !
Been a while, Andre !
Single-handedly, your e-commerce video series made me an intermediate Laravel developer.
Returning to say "Thank you" !