小程序转uniapp,地图页面改为nvue页面。业务为请求回来的定位,要替代原来的定位 ,按照下程序的代码走不通。不显示图片

let mark = {
   iconPath: "/static/common/images/current_location@3x.png",
   id: 1001,
   latitude: wgs84togcj02[1],
   longitude: wgs84togcj02[0],
   width: 50,
   height: 50
};
let markers = that.markers
for (var index = 0; index < markers.length; index++) {
	if (markers[index].id == '1001') {//业务逻辑
       markers.splice(index, 1);
       break;
     }
}
markers.push(mark);
that.markers = markers

以上写法无效,原因是 marker是Array类型,赋值的时候只能对整个数组进行更改赋值,不能只改变内部的对象,改为下面的写法可行

let mark = {
   iconPath: "/static/common/images/current_location@3x.png",
   id: 1001,
   latitude: wgs84togcj02[1],
   longitude: wgs84togcj02[0],
   width: 50,
   height: 50
};
let markers = []
for (var index = 0; index < that.markers.length; index++) {
	if (that.markers[index].id == '1001') {
       that.markers.splice(index, 1);
       break;
     }
}
markers.push(mark);
markers.concat(that.markers)
that.markers = markers

不清楚是uniapp的问题还是nvue的问题,或者应该是自己代码写法的问题。本不应该直接复用markers,需要合理赋值继承

解决问题灵感来自:https://www.cnblogs.com/swordLaughsHeaven/p/13195601.html

Logo

前往低代码交流专区

更多推荐