Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Advanced Composables, Plugins & Architecture 18 mins read Free Lesson

Custom Vue 3 Directives & Plugin Architecture

Create custom directives like v-click-outside, v-focus, and build reusable Vue application plugins.

Custom Directives (v-click-outside)

export const clickOutsideDirective = {
    mounted(el, binding) {
        el.clickOutsideEvent = function(event) {
            if (!(el === event.target || el.contains(event.target))) {
                binding.value(event)
            }
        }
        document.addEventListener('click', el.clickOutsideEvent)
    },
    unmounted(el) {
        document.removeEventListener('click', el.clickOutsideEvent)
    }
}