先看代码
computed: {
fullName: {
// getter
get: function() {
return this.firstName + ' ' + this.lastName
},
// setter
set: function(newValue) {
console.log('9999');
var names = newValue.split(' ')
this.firstName = names[0]
}
}
},
解释:代码中,fullName 的变化依赖于firstName 和lastName ,当二者变化的时候,会自动计算出 fullName 。 computed中的set 的意义在于,当用户强制更改赋予fullName值的时候,我也可以根据此新值firstName 和lastName做对应赋值操作。对是的,就是当时 当你手动设置 fullName的时候。console.log(999才会执行。