📘

Vueのwatch

に公開
2

Vueのwatchの仕様について、イマイチ理解していない。サンプルコードを書いて、仕様について理解する。

サンプルコード

<script setup>
import {ref, watch} from 'vue'

const count = ref(0)
watch(count, (newValue, oldValue) => {
  console.log(oldValue);
  console.log(newValue);
})
</script>

<template>
  <button type="button" @click="count++">count is {{ count }}</button>
</template>

仕様

第1引数

  • リアクティブデータを指定する。

第2引数

  • 関数を指定する。

関数の第1引数

  • 変更後(newValue)のリアクティブデータが設定される。

関数の第2引数

  • 変更前(oldValue)のリアクティブデータが設定される。

Discussion

senahasenaha

ありがとうございます。
不要な処理が入っていたので、修正しました。