[Vuejs]-How to use Vue watch to observe a form item value?

3👍

To watch data changes using the watch object, add a function to that object with a name matching the data property to watch. For instance, to watch a data property named "question", add this function:

watch: {
  question(newValue, oldValue) {
    //...
  }
}

Alternatively, you could add an object named after the target data property, with a handler function and additional watcher options: deep and immediate.

watch: {
  question: {
    deep: true,      // detecting nested changes in objects
    immediate: true, // triggering the handler immediately with the current value
    handler(newValue, oldValue) {
      //...
    }
  }
}

In your case with "timeRange", the syntax would be:

watch: {
  timeRange(newValue) {
    //...
  }
}

demo

👤tony19

Leave a comment