computed 和 watch 区别

computed 是计算属性,依赖其他属性计算值,并且 computed 的值有缓存,只有当计算值变化才会返回内容。

watch 监听到值的变化就会执行回调,在回调中可以进行一些逻辑操作。

所以一般来说需要依赖别的属性来动态获得值的时候可以使用 computed,对于监听到值的变化需要做一些复杂业务逻辑的情况可以使用 watch

另外 computed 和 watch 还都支持对象的写法

vm.$watch('obj', {

// 深度遍历

deep: true,

// 立即触发

immediate: true,

// 执行的函数

handler: function(val, oldVal) {}

})

var vm = new Vue({

data: { a: 1 },

computed: {

aPlus: {

// this.aPlus 时触发

get: function () {

return this.a + 1

},

// this.aPlus = 1 时触发

set: function (v) {

this.a = v - 1

}

}

}

})

猜你喜欢

转载自www.cnblogs.com/wan20170426/p/11687602.html
今日推荐