通過(guò)watch監(jiān)聽一個(gè)數(shù)組的變化蝶糯,發(fā)現(xiàn)打印的newVal和oldVal的值一模一樣辆沦,舊值內(nèi)容跟新值內(nèi)容一樣。
解決辦法:用一個(gè)計(jì)算屬性返回?cái)?shù)組的值肢扯,再去監(jiān)聽這個(gè)計(jì)算屬性妒茬,在watch里面就可以得到變化前的舊值了鹃彻。
computed: {
curInterIds() {
return [...this.interIds];
}
},
watch: {
curInterIds: function (newVal, oldVal) {
newVal = newVal || [];
oldVal = oldVal || [];
newVal.forEach(interId => {
// 新的有蛛株,舊的沒(méi)有
if (!oldVal.includes(interId)) {
this.delPoint(this.roadLayerName, interId);
let roadInfo = this.roadListMap.get(interId);
this.addSelRoadPoint(roadInfo);
}
});
oldVal.forEach(interId => {
// 新的無(wú),舊的有
if (!newVal.includes(interId)) {
this.delPoint(this.roadLayerName, interId);
let roadInfo = this.roadListMap.get(interId);
this.addRoadPoint(roadInfo);
}
});
}
},