当我在重复的 div 中有一个单击处理程序时v-for,似乎在该单击处理程序中所做的更改不会在 DOM 中更新。
为什么?
https://jsfiddle.net/AndersBillLinden/109uzsx7/27/
html:
<div id="vue">
<input type="checkbox" v-model="force_update"/> force update
<div v-for="e in arr">
{{e.id}} = {{e.text}}
<a href="#" @click="on_link_clicked(e)">click</a>
<span v-show="e.clicked"> clicked</span>
</div>
</div>
js:
new window.Vue(
{
el: '#vue',
data:
{
arr: [{id:1,text:"one"}, {id:2, text:"two"}, {id:3, text:"three"}],
force_update: true
},
methods:
{
on_link_clicked(e)
{
e.clicked = true;
if (this.force_update)
this.$forceUpdate();
}
}
});
点击链接1
取消选中强制更新
点击第二个链接
(什么都没发生)
检查“强制更新”
现在呈现上一步中的更改。
结论是我们有时需要强制更新,但不清楚原因。




