directives: {
clickoutside: {bind(el, binding, vnode) {function documentHandler(e) {
// 如果是元素本体则返回
if (el.contains(e.target)) return false;
if (binding.expression) {
// 有绑定函数,则执行
binding.value(e);
}
}
el.__vueClickOutside__ = documentHandler;
document.addEventListener('click', documentHandler);
},
update() {},
unbind(el, binding) {document.removeEventListener('click', el.__vueClickOutside__);
delete el.__vueClickOutside__;
},
},
}
应用
<div v-clickoutside="hideTree"></div>