问题是 showGallery 属性默认为false,但是循环后,所有的都是false, 如果点击某一个gallery, 所有的showgallery 都变成了ture, 所以只会显示最上层的那个galler(别的被覆盖在底层)。这里需要吧每个循环项,单独设置ture or false,相互之间不影响,应该怎么操作呢?项目是, 循环项目里,会循环 一个图片 和一个组件, 点击对应的图片回弹出对应的gallery组件。在点击回隐藏组件。<div v-for="item of list":key="item.id"><div class="item"> <img class="item-img " :src="item.imgUrl" @click="handleGalleryClick($index)"> </div><CommonGallery:imgs="item.screenshots"v-show="showGallery"@close="handleGalleryClose"></CommonGallery></div> </div></template><script>import CommonGallery from "./../../../common/gallery/Gallery";export default {name: "ProjectList",components: {CommonGallery},props: {list: Array},data() {return {showGallery: false};},methods: {handleGalleryClick() {this.showGallery= true;},handleGalleryClose() {this.showGallery = false;}}};</script>
2 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
把showGallery维护成一个数组就能实现了
< img class = "item-img " :src = "item.imgUrl" @ click = "handleGalleryClick(item.id)" > < CommonGallery :imgs = "item.screenshots" v-show = "showGallery.includes(item.id)" @ close = "handleGalleryClose(item.id)" > </ CommonGallery > |
data() { return { showGallery: [], }; }, methods: { handleGalleryClick(id) { this .showGallery.push(id); }, handleGalleryClose(id) { const index = this .showGallery.indexOf(id); this .showGallery.splice(index, 1); }, |
添加回答
举报
0/150
提交
取消