element-plus el-popover 点击其他区域关闭弹出框

场景描述

使用trigger="click"时,也可以做到点击其他区域关闭弹出框,但是当按钮是禁用状态也会显示弹出框,这时候需要使用虚拟触发来解决。

在这里插入图片描述

解决方案:

<template>
	<el-tooltip :disabled="hasSelection" effect="dark" content="请勾选" placement="top">
		<span>
			<el-button ref="buttonRef" v-click-outside="hide" :disabled="!hasSelection" type="primary" class="ml15" >
				{{ props.name }}
			</el-button>
		</span>
	</el-tooltip>
	<el-popover
		ref="popoverRef"
		:virtual-ref="buttonRef"
		trigger="click"
		virtual-triggering
		placement="top-start"
		:width="200"
		:height="200"
	>
		<div>
			content
		</div>
	</el-popover>
</template>

<script setup lang="ts" name="popoverUlButton">
import { defineAsyncComponent, reactive, toRefs, onMounted, defineComponent, ref, getCurrentInstance, computed } from 'vue';
import { ElMessage, FormInstance, ClickOutside as vClickOutside } from 'element-plus';
import { Search } from '@element-plus/icons-vue';
//ref
const buttonRef = ref();
const popoverRef = ref();

interface State {
	visible: boolean;
}

const props = defineProps({
	name: {
		type: String,
		default: '',
	},
	hasSelection: {
		type: Boolean,
		default: false,
	},
	dropdownList: {
		type: Array,
		default: [],
	},
	prop: {
		type: String,
		default: '',
	},
});

const state = reactive<State>({
	visible: false,
});


const hide = () => {
	popoverRef.value.hide();
};

onMounted(() => {});

// 暴露给父组件
defineExpose({});
</script>

<script lang="ts">
export default {};
</script>

<style scoped>
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值