Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue 'watch' Option


Example

Using a watcher inside the watch option to make it impossible to choose values between 20 and 70 with an <input type="range">.

export default {
  data() {
    return {
      rangeVal: 4
    };
  },
  watch: {
    rangeVal(val) {
      if( val>20 && val<70) {
        if(val<40){
          this.rangeVal = 20;
        }
        else {
          this.rangeVal = 70;
        }
      }
    }
  }
};
Run Example »

Definition and Usage

The watch option is an object with all the watchers that are declared on the Vue instance.

A watcher is a function with the same name as a data property or a computed property. The watcher is called automatically whenever that property with the same name gets changed.

When a watcher is called, the new and the previous values are available as arguments to the watcher function.

A watcher can also be a dot-delimited path, such as tiger.weight, so that the watcher is only called when the weight property of the tiger object is changed.

Note: Arrow functions should be avoided when declaring watchers because the Vue instance cannot be reached from inside such a function using the this keyword.

When writing watchers using the object syntax (see the example below), these options are available:

Option Description
handler This is where the watch function is written.
'method name' A watcher can be set up to call a method by providing the method name as a string.
deep Default value is 'false'. If the watcher is deep, it also reacts to changes further down in the property the watcher is set up to watch.
immediate Default value is 'false'. Triggers the watcher immediately after it is created. The old value will be 'undefined' the first time the watcher is triggered when 'immediate' is set to 'true'.
flush Default value is 'pre'. Specify when to run the callback function relative to when the component is rendered. Possible values are 'pre', 'post' and 'sync'. Use this flush option with caution.
onTrigger/onTrack Used for debugging. Only works in development mode.

Note: Watchers can also be created using the $watch() method.


More Examples

Example

Using a watcher with the object syntax.

<template>
  <h2>Example watch Option</h2>
  <p>The 'rangeVal' watcher is written with the object syntax, with immediate: true, so that rangeVal is moved to '70' when the page first loads:</p>
  <input type="range" v-model="rangeVal">
  <p>rangeVal: <span>{{ rangeVal }}</span></p>
</template>

<script>
export default {
  data() {
    return {
      rangeVal: 40
    };
  },
  watch: {
    rangeVal: {
      handler(val) {
        if (val > 20 && val < 70) {
          if (val < 40) {
            this.rangeVal = 20;
          }
          else {
            this.rangeVal = 70;
          }
        }
      },
      immediate: true
    }
  }
};
</script>

<style>
span {
  padding: 3px;
  font-weight: bold;
  font-family: 'Courier New', Courier, monospace;
  background-color: lightgreen;
}
</style>
Run Example »

Related Pages

Vue Tutorial: Vue Watchers

Vue Tutorial: Vue v-model Directive

Vue Reference: Vue $watch() Method


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.