Difference between calling watcher with parentheses vs without

35 Views Asked by At

In watch property, what is the difference between calling property using () and not using them?

export default {
  watch: {
    // syntax with ()
    test() {
      if (this.test) {
        //some work
      } else {
        //some work
      }
    },

    // syntax without ()
    labels: {
      immediate: true,
      deep: true,
      handler(option) {
        //some work
      }
    }
  }
}

Futhermore what is the aim of immediate and deep?

1

There are 1 best solutions below

0
ayitinya On

watch is lazy by default, meaning it would only run after an initial change to the source has occurred.

adding immediate: true essentially let's the callback of the watch to run when the component is initialized. In essence, like the do block of a do while loop in certain languages.

you can find more and the deep and eager watch at the docs https://vuejs.org/guide/essentials/watchers.html