Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use Two-Way Binding in Vue Using @Model Decorator with TypeScript


    Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as forms. This lesson shows how to use it by creating a custom checkbox component using the @Model decorator in TypeScript.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Here we have an empty my checkbox component, which we will call later. For now, let's import it in app component. Then we need to add it to the component property of the decorator, so then we can use it. Now, let's create a checkbox object.

    We will have a title to show in the checkbox, a value, which should be a unique identifier, and a checked property. These will be the properties to pass to the component. Let's go up to the template, and use my checkbox component.

    We are going to write a title property, and use the Vue model for the checked property of the checkbox. That way, it will be bound. Now that we have set this here, let's go to my checkbox component, and write here a basic input for a checkbox. We will print the title besides.

    Now, we have to take the properties. By using the property decorator, let's take the title and a value property. We didn't pass any value property, but Vue model creates it for accessing its value. Let's use it in the checked property of the input.

    If we ran it, we see that the property, it seems to be passed down as the checkbox is checked. When we toggle it, we see in the Vue developer tools that the checked property doesn't change. That's because we're still emitting from the parent component that the property is changing.

    For that, let's listen to the change event of the input. In there, we need to emit an input event passing the check value that we take from the event itself. Vue model implicitly listens to the input event, and will update the checked value.

    Let's run this again. Now, if we toggle the checkbox, we see the checked property is changing, so this is working. In some cases, you may find the problem with Vue model. This is one of them. If we go back to the app component, we have a value property that we are going to pass down.

    Vue model implicitly sets a value property and listens to an input event. We need to change that. Let's pass our value property here. Then in my checkbox component, we can use the model decorator to tell Vue which property and event to use.

    In this line, we are saying use the change and the checked property, which is a Boolean. Now, we can replace the input for the change event. Don't forget to use the property decorator as well before the model, because checked is a property. If we don't do it, it will not be considered as so.

    Now, we can change the checked property to use the new name, and write the value property that we are passing. Let's run this one more time. Now, we'll see the checked property is working back again. If we inspect the HTML element, we see the value property is being passed correctly.