通过watch监听一个数组的变化,发现打印的newVal和oldVal的值一模一样,旧值内容跟新值内容一样。
解决办法:用一个计算属性返回数组的值,再去监听这个计算属性,在watch里面就可以得到变化前的旧值了。
computed: {
curInterIds() {
return [...this.interIds];
}
},
watch: {
curInterIds: function (newVal, oldVal) {
newVal = newVal || [];
oldVal = oldVal || [];
newVal.forEach(interId => {
// 新的有,旧的没有
if (!oldVal.includes(interId)) {
this.delPoint(this.roadLayerName, interId);
let roadInfo = this.roadListMap.get(interId);
this.addSelRoadPoint(roadInfo);
}
});
oldVal.forEach(interId => {
// 新的无,旧的有
if (!newVal.includes(interId)) {
this.delPoint(this.roadLayerName, interId);
let roadInfo = this.roadListMap.get(interId);
this.addRoadPoint(roadInfo);
}
});
}
},