iT邦幫忙

0

Vue.js [watch]: 物件屬性如何用 watch

遇到問題

在使用 Vue 的 watch 的時候,一般都是用這樣的

watch:{
    text:{
        handler(newValue){
            console.log(newValue)
        }
    }
}

這樣就可以使用 watch 觀察 text 這個變數。
但是如果想要觀察這個物件的 title 的話就不知道怎麼做

message:{
    title:'',
    container:'',
    footer:'',
}

解決方法

  • Vue 想要觀察物件的話可以這樣做
message:{
    title:'',
    container:'',
    footer:'',
}

只要在觀察的變數上加入 '' "單引號"就可以了

watch:{
    'message.title':{
        handler(newValue){
            console.log(newValue)
        }
    }
}

回上一頁
回下一頁


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言