思路:
在 popover 组件的 show
方法中, 注册一个 document
的点击事件, 在该事件回调方法中, 判断点击的是否为 popover 组件外的空白区域, 若是, 则关闭 popover 组件.
注:
要在关闭回调事件中移除事件, 防止重复注册监听.
<el-popover
ref="popoverEl"
trigger="manual"
placement="bottom-end"
v-model="visible"
:width="width"
@show="show"
@hide="close"
:append-to-body="appendToBody"
>
<div
class="popover-tree-el-title"
slot="reference"
:style="setTitleStyle"
@click="showOrHidePopoverEl"
>
<div class="popover-tree-el-title-text click-text">
<i class="el-icon-edit-outline l-mr-10"></i>
<span>{{ popoverElTitle }}</span>
</div>
</div>
</el-popover>
<script>
export default {
methods: {
show() {
document.addEventListener('click', this.hidePanel, false)
},
close() {
document.removeEventListener('click', this.hidePanel, false)
this.visible = false
},
// 点击空白区域隐藏面板事件
hidePanel(e) {
const isOtherArea =
!e.target.parentElement.className.includes('click-text')
if (isOtherArea) this.close()
},
},
}
</script>
```