Become a member
to unlock all features

Level Up!

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


    Use Vue.js Watchers to Respond to Async Updates

    Greg ThomanGreg Thoman

    Use watchers to keep an eye on your data. Watchers are methods that are invoked when the specified attribute changes. They are useful when you want to perform asynchronous operations in response to changing data.



    Become a Member to view code

    You must be a 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




    Here's a form that, when submitted, adds a new dinosaur to a list. Alongside our data block, let's add a new block called watch. We already have our input field bound to the attribute input. Let's watch that value by naming our watcher input.

    Watchers are functions. We'll debounce this one, and then set this.buttonText to a ternary. If this.input is not empty, let's change the button text to "Add" and whatever the input is. If it is empty, change it back to "Add Dinosaur." Let's debounce this over 250 milliseconds.

    Now, when the input value changes, our new watcher is invoked, and the button text is changed to reflect it.