【发布时间】:2020-01-25 00:30:56
【问题描述】:
我对 Vue 比较陌生,我想知道我的组件有什么问题,我的 isHover 变量(prop?)无法改变鼠标悬停时的背景。
<template>
<div class="list-wrap" v-if="gridItems">
<div
class="list-itme"
v-for="(item, index) in gridItems"
:key="index"
@click.stop="setCurrentLocation(location)"
>
<a
@mouseover="mouseOver(index)"
@mouseleave="mouseLeave(index)"
:style="{
background: isHover
? `url(${item.location_image.thumbnails.large.url})`
: `url(${item.location_image.thumbnails.largeHover.url})`
}"
>
{{ item.location_name }}
{{ isHover }}
</a>
</div>
</div>
</template>
<script>
export default {
name: "GridItems",
computed: mapState(["filters", "GridItems"]),
methods: {
mouseOver(index) {
this.item[index].isHover = true;
},
mouseLeave(index) {
this.item[index].isHover = false;
}
},
data() {
return {
isHover: false
};
}
};
</script>
【问题讨论】:
-
我可以澄清一下,如果鼠标悬停在项目上,您想更改项目的背景图像吗?
-
既然简单的 CSS 就足够了,为什么还要用 Vue 来做呢?
-
@stephenthomas 因为每个背景图像对于每个项目都是唯一的,通过 API 拉取。
-
@evan 是的,项目的背景图片。图片是通过 api 在循环中设置的,每个项目。
-
@StephenThomas 你说得对,我在示例中愚蠢地使用了颜色来保持简单,但我只是更改了它以反映真实情况。