偵測變化 - watch
Watch 文檔地址
// watch 簡單應用
watch(data, () => {
document.title = 'updated ' + data.count
})
// watch 的兩個參數,代表新的值和舊的值
watch(refData.count, (newValue, oldValue) => {
console.log('old', oldValue)
console.log('new', newValue)
document.title = 'updated ' + data.count
})
// watch 多個值,返回的也是多個值的數組
watch([greetings, data], (newValue, oldValue) => {
console.log('old', oldValue)
console.log('new', newValue)
document.title = 'updated' + greetings.value + data.count
})
// 使用 getter 的寫法 watch reactive 對象中的一項
watch([greetings, () => data.count], (newValue, oldValue) => {
console.log('old', oldValue)
console.log('new', newValue)
document.title = 'updated' + greetings.value + data.count
})