Created
August 16, 2020 18:25
-
-
Save ColtHands/ff90fcec252b2a606bf3405a7eae11f8 to your computer and use it in GitHub Desktop.
Vue click outside element directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.directive('click-outside-element', { | |
bind(el, bind, vn) { | |
el.cO = event => { | |
if (!(el == event.target || el.contains(event.target))) { | |
if(vn.context[bind.expression]) { | |
vn.context[bind.expression](event) | |
} | |
} | |
} | |
document.body.addEventListener('click', el.cO) | |
}, | |
unbind(el) { | |
document.body.removeEventListener('click', el.cO) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment