【发布时间】:2023-03-06 11:46:02
【问题描述】:
我想制作一个当鼠标悬停在图像上时获取文本框的组件。
下面是 HTML 模板。
<section class="item-container" v-for="(item, index) in items">
<div class="image-box" @mouseenter="changeStatus(index)">
<img class="image" src="item.link" alt>
</div>
<div class="text-box" @mouseleave="changeStatus(index)" v-if="show[index]">
<h4>{{ item.name }}</h4>
<p>{{ item.content }}</p>
</div>
</section>
下面是app.js
new Vue({
el: '#app',
data: {
show: [false, false, false],
items: [
{
name: 'Author 1',
content: 'Content 1'
},
{
name: 'Author 2',
content: 'Content 2'
},
{
name: 'Author 3',
content: 'Content 3'
}
]
},
methods: {
changeStatus: function(index) {
this.show[index] = !this.show[index];
console.log(this.show);
console.log(this.show[index]); // console gets it as expected
}
}
});
当我执行上面的代码时,我可以发现 show 属性发生了变化。但是,v-if 没有更新。我们不能将 array[index] 用于 v-if 还是有其他原因?
【问题讨论】:
-
您是否反对在每个
item上使用布尔值而不是单独的show数组?