解決問(wèn)題:TreeSelect組件初次點(diǎn)擊面板消失
結(jié)合TreeSelect組件 alwaysOpen 屬性
<template>
<div>
<div class="show" v-show="show" v-clickoutside="handleClose">
顯示
</div>
</div>
</template>
<script>
const clickoutside = {
// 初始化指令
bind(el, binding, vnode) {
function documentHandler(e) {
// 這里判斷點(diǎn)擊的元素是否是本身,是本身,則返回
if (el.contains(e.target)) {
return false;
}
// 判斷指令中是否綁定了函數(shù)
if (binding.expression) {
// 如果綁定了函數(shù) 則調(diào)用那個(gè)函數(shù),此處binding.value就是handleClose方法
binding.value(e);
}
}
// 給當(dāng)前元素綁定個(gè)私有變量,方便在unbind中可以解除事件監(jiān)聽(tīng)
el.__vueClickOutside__ = documentHandler;
document.addEventListener('click', documentHandler);
},
update() {},
unbind(el, binding) {
// 解除事件監(jiān)聽(tīng)
document.removeEventListener('click', el.__vueClickOutside__);
delete el.__vueClickOutside__;
},
};
export default {
name: 'HelloWorld',
data() {
return {
show: true,
};
},
directives: {clickoutside},
methods: {
handleClose(e) {
this.show = false;
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.show {
width: 100px;
height: 100px;
background-color: red;
}
</style>
參考文章:https://blog.csdn.net/heynewbie/article/details/106883412