- 计算显示值
- 缓存显示值
- 响应计算(依赖 reactive data)
三大特征,自己体会体会。
Computed Setters
计算属性默认只有 getter ,不过在需要时你也可以提供一个 setter :
// ...
computed: {
fullName: {
// getter
get: function () {
return this.firstName + ' ' + this.lastName
},
// setter
set: function (newValue) {
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
// ...
现在再运行 vm.fullName = 'John Doe' 时, setter 会被调用, vm.firstName 和 vm.lastName 也相应地会被更新。
Computed vs Watcher
Computed 和 Watcher 同样是依赖 reactive data 做出响应,但 computed 本身也是一个属性值,属性值是同步计算,而对于响应后的计算操作则应该放到 watcher 中。