sleepyShen1989 / blog

MIT License
0 stars 0 forks source link

【实战】computed & watch 查缺补漏 #10

Open sleepyShen1989 opened 2 years ago

sleepyShen1989 commented 2 years ago

computed

methods区别:模块中多次使用的值时重新渲染(包括第一次渲染)时,computed只执行一次

setter和getter

值为函数的话,该函数是该key的getter方法

watch

// 深度侦听/立即执行
watch: {
    info: {
        handler: function (newInfo, oldInfo) {
            console.log(newInfo)
            console.log(oldInfo)
        },
        deep: true,
        immediate: true
    },
    'info.name'(newValue) {
    }
}

const unwatch = this.$watch("info", (newValue, oldValue)=>{
    console.log(newValue)
}, {
    deep:true,
    immediate: true
})

// 取消侦听
unwatch()