关于 element 中循环 popover 手动控制展示问题

在开发过程中 经常会遇到这种情况 

循环了 N 条数据,每条数据都有单独的 popover 弹出层 

    <div v-for="item in 4">
      <el-popover placement="bottom" :title="`标题${item}`" width="200" trigger="manual" content="这是一段内容。" v-model="visible">
        <el-button slot="reference" @click="visible = !visible">手动激活</el-button>
      </el-popover>
    </div>

如果这样的话,会出现点击一个按钮,全部的 popover 都会出现,再点击,全部都会消失。

这样的话就不符合需求了,所以说以上代码需要修改。

产生这样问题的原因是因为一个布尔值已经无法控制多个弹出层的展示了。

思路:可以修改为多个布尔值控制(数组),或者修改成一个对象控制,或者用 id 判断

以下是我得实现方式

1     <div v-for="item in 4" :key="item">
2       <el-popover placement="bottom" :title="`${item}`" width="200" trigger="manual" content="这是一段内容。" :value="funPopover(item)">
3         <el-button slot="reference" @click="funaaaa(item)">手动激活{{item}}</el-button>
4       </el-popover>
5     </div>
 1  data() {
 2     return {
 3       id: null
 4     };
 5   },
 7   methods: {
 8     funaaaa(item) {
 9       if (this.id == item) {
10         this.id = null;
11       } else {
12         this.id = item;
13       }
14     },
15     funPopover(item) {
16       return item == this.id;
17     }
18   }

一个布尔值无法判断的话,跟列表一样,用一个 id 判断,

点击时候给 id 赋值,再次点击给 id 置空,因为控制显示隐藏的是 v-model 并且后边无法跟函数,所以修改为 value 控制,后边跟一个函数,用来判断,得到布尔值。

 

posted @ 2025-07-22 18:03  马文庆i  阅读(102)  评论(0)    收藏  举报