当前位置:   article > 正文

el-popover中嵌套使用el-select展开时自动关闭的解决方法_vue3 el-popover点击外面会关闭

vue3 el-popover点击外面会关闭

el-popover中嵌套使用el- select,当鼠标移除el-popover一秒后关闭el-popover。
1、el-popover使用手动模式

<el-popover trigger="manual" v-model="visible">
	<div ref="popoverRef">
		<el-select v-model="value1"></el-select>
		<el-select v-model="value2"></el-select>
	</div>
	<el-button slot="reference" @click="visible=!visible"></el-button>
</el-popover>
// javascript
this.$refs.popoverRef.addEventListener('mouseleave', (e) => {
	this.time = 0;
	setTimeout(() => {
		++this.time;
		if (this.time == 1) {
			this.visible = false;
		}
	}, 1000)
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里会出现一个问题!el-select选项展开的时候它是插入body的,所以选择选项的时候会移除popoverRef的范围
这里我加了一个字段判断select是否打开,通过select的visible-change的值来修改

	<el-select v-model="value1"  @visible-change="selectStateChange"></el-select>
	// javascript
	selectStateChange (val) {
   	  this.selectOpen = val;
   },
  • 1
  • 2
  • 3
  • 4
  • 5

当鼠标快速移出再移入的时候则需要取消计时。
完整的代码如下:

<el-popover trigger="manual" v-model="visible" @show="onShowPopover" @hide="onHidePopover">
   <div ref="popoverRef">
   	<el-select v-model="value1" @visible-change="selectStateChange"></el-select>
   	<el-select v-model="value2" @visible-change="selectStateChange"></el-select>
   </div>
   <el-button slot="reference" @click="visible=!visible"></el-button>
</el-popover>
// javascript
onShowPopover () {
   this.$refs.popoverRef.addEventListener('mouseleave', (e) => {
   	this.time = 0;
   	this.timeOut = setTimeout(() => {
   		++this.time;
   		if (this.time == 1 && !this.selectOpen && !this.inPoPover) {
   			this.visible = false;
   		}
   	}, 1000)
   })
   this.$refs.popoverRef.addEventListener('mouseenter', (e) => {
   	this.inPoPover = true;
   })
}
onHidePopover () {
   clearTimeout(this.timeOut);
}
selectStateChange (val) {
    this.selectOpen = val;
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号