【发布时间】:2020-07-05 02:50:14
【问题描述】:
我正在尝试使用 Vue 在图像上添加一个简单的悬停效果,以便悬停显示图像数组中的第二项。如果数据已更改,我无法找到不断更新模板数据的最佳方法。我试过 Computed,对我也不起作用。
console.log() 中的所有方法都可以正常更新。但不适用于观察者或计算者。
我看到手表只能在 Mount 上工作,但不能在 mouseenter 上工作。
<div class="cursor-pointer" v-for="(image, index) in images" :key="index"
@mouseenter="hoverEnter(index)"
@mouseleave="hoverLeave(index)"
>
// MOUSE OVER IS NOT UPDATING
<div><img :src="mouseOver[index] ? image[1].src : image[0].src"></div>
</div>
data() {
return {
id: this.$route.params.id,
images: [],
mouseOver: []
};
},
mounted() {
this.returnImages
this.mouseOver = this.currentCollection.products.map((res) => {
return false
})
},
methods: {
hoverEnter(index) {
this.mouseOver[index] = true
},
hoverLeave(index) {
this.mouseOver[index] = false
},
},
watch: {
mouseOver: function (newValue) {
console.log(newValue) // THIS IS NOT UPDATING
}
},
【问题讨论】: