- watch一個(gè)object類型的變量時(shí),如下面例子的a.b
a={
b: {
b1: 1,
b2: 2
}
}
@Watch('a.b', { deep: true })
async onValueChange(newVal, oldVal) {
// a.b.b1 值改變時(shí)
// newVal.b1 === oldVal.b1 是意料之外的true,因?yàn)閛bject的js特性,引用變了,oldVal已經(jīng)更新
}
解決:
get a_b() {
// to confirm the oldValue is indeed old value
return Object.assign({}, this.a.b)
}
async onValueChange(newVal, oldVal) {
// newVal.b1 === oldVal.b1 可以正常判定
}