简体   繁体   中英

Difference between watch() and watch:{}?

I am new on VueJs3 and following a tutorial and I am confused about watch
From tutorial there used watch() but I used like watch: {} before so my question is what is the difference between them and how can I use following code inside watch: {} ?

watch(
  () => props.guessedLetters,
  (guessedLetters, prevGuessedLetters) => {
    keyboard.value.addButtonTheme(
      guessedLetters.miss.join(" "), 
      "miss"
    );
    keyboard.value.addButtonTheme(
      guessedLetters.found.join(" "), 
      "found"
    );
    keyboard.value.addButtonTheme(
      guessedLetters.hint.join(" "), 
      "hint"
    );
  },
  { deep: true }
);

There is no difference in how the watchers work. The watch() syntax is used in Composistion API and watch: {} syntax is used in Option API . The watcher you have (converted to Option API syntax) should look something like this:

watch: {
  guessedLetters: {
    handler(guessedLetters, prevGuessedLetters) {
      keyboard.value.addButtonTheme(
        guessedLetters.miss.join(" "), 
        "miss"
      );
      keyboard.value.addButtonTheme(
        guessedLetters.found.join(" "), 
        "found"
      );
      keyboard.value.addButtonTheme(
        guessedLetters.hint.join(" "), 
        "hint"
      );
    },
    deep: true
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM