Javascript 为什么我的v-for语句对Vue的反应不正确?
我有以下代码Javascript 为什么我的v-for语句对Vue的反应不正确?,javascript,arrays,vuejs2,Javascript,Arrays,Vuejs2,我有以下代码 <marker-popup v-for="point in pointsArray" :position="point.latlng" :title="point.name" > </marker-popup> 但是,它不会影响v-for语句。每当addPoint()方法运行时,它都会通过以下两种方式之一更改pointsArray 它推到阵列上-这很好,v-for是完美的 它根据Vue.js文档的建议更改数组中的元素。这根本不起作用。我的console.l
<marker-popup v-for="point in pointsArray" :position="point.latlng" :title="point.name" > </marker-popup>
但是,它不会影响v-for语句。每当addPoint()方法运行时,它都会通过以下两种方式之一更改pointsArray
我想,我可以删除元素,然后推动它,但这种方法似乎很笨拙,根据他们的文档,这应该可以工作。结果表明,我做的一切都是正确的,但Vue 2.0不能很好地发挥作用,并且拒绝做出正确的反应。切换到一个解决了所有问题。Array.find在数组中查找元素本身,而不是索引。还有,为什么要维护两个版本的状态?数组上的一个好点。find@BertEvans。不过,这并没有影响我的代码-console.log语句仍然显示它实际上是在修改arrayv for,而不能遍历字典-我需要一种创建数组的方法。我尝试创建一个计算属性,将字典转换为数组,但它始终使用计算机属性的缓存版本,即使我在Dictionaryk上使用this.$set函数,那么
v-for
可以迭代对象,而computed
应该在这里工作。让我看看。
<template>
<l-marker :position="position" :title="title" :draggable="false">
<l-popup :content="text"></l-popup>
</l-marker>
</template>
<script>
export default {
name: 'MarkerPopup',
props: ['position','title'],
computed: {
text: function(){
return "<b>" + this.title + "</b><br>"
+ this.position[0] + ", " + this.position[1];
}
}
}
</script>
<style lang="scss">
</style>
addPoint: function(data) {
let alreadyExists = false;
if(this.pointsDictionary[data.uid]!=undefined){
alreadyExists = true;
}
this.pointsDictionary[data.uid] = {};
this.$set(this.pointsDictionary,data.uid,{
'name': data.name,
'latlng': data.latlng,
'uid': data.uid
});
// this.pointsDictionary[data.uid]['name'] = data.name;
// this.pointsDictionary[data.uid]['latlng'] = data.latlng;
// this.points[data.uid]["marker"] = null;
if(alreadyExists){
console.log("exists");
var index = this.pointsArray.find(function(point){
return point.uid == data.uid;
});
//this.$set(this.pointsArray,index,this.pointsDictionary[data.uid]);
this.pointsArray.splice(index,1,this.pointsDictionary[data.uid]);
}
else {
this.pointsArray.push(this.pointsDictionary[data.uid]);
}
console.log(JSON.stringify(this.pointsDictionary));
console.log(JSON.stringify(this.pointsArray2()));
}