0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

watchの使い方 Vue.js

0
Posted at

watchの使い方

watch(監視対象, 監視対象が変化したときに実行する関数)

watch のコールバックには2つの値を受け取ることができます。

watch(totalThisMonthAmount, (newAmount, oldAmount) => {
  console.log('変更前:', oldAmount)
  console.log('変更後:', newAmount)
})

例えば、

変更前: 45000
変更後: 55000

となります。

なので、

newAmount → 変更後の値
oldAmount → 変更前の値

と覚えておけばOK。

実際の使用例

watch(totalAmount, (newAmount) => {
  if (newAmount > 50000) {
    console.log('今月の支出が5万円を超えました')
  }
})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?