You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
722 B
23 lines
722 B
const clickoutside = {
|
|
bind(el, binding, vnode) {
|
|
function documentHandler(e) {
|
|
// 这里判断点击的元素是否是本身,是本身,则返回
|
|
if (el.contains(e.target)) {
|
|
return false
|
|
}
|
|
// 判断指令中是否绑定了函数
|
|
if (binding.expression) {
|
|
binding.value(e)
|
|
}
|
|
}
|
|
// 给当前元素绑定个私有变量,方便在unbind中可以解除事件监听
|
|
el.__vueClickOutside__ = documentHandler
|
|
document.addEventListener('click', documentHandler)
|
|
},
|
|
unbind(el, binding) {
|
|
// 解除事件监听
|
|
document.removeEventListener('click', el.__vueClickOutside__)
|
|
delete el.__vueClickOutside__
|
|
}
|
|
}
|
|
export default clickoutside
|
|
|