Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Vue directive to open a modal

I am wanting to create a vue directive that can be added to any HTML element in which clicking it would attach a click event to open a "related" modal component.

My directive looks like this,

    import { DirectiveOptions, DirectiveBinding } from 'vue/types/options';

    const handleDialogClose = () => {
        console.log("do close");
    };

    const dialogShow: DirectiveOptions = {
        bind(el: HTMLElement, binding: DirectiveBinding, vnode: any) {
        //add a keycode listerner to close dialog on "escape"
        document.body.addEventListener('keyup', handleDialogClose);

        //change the current dialogs data to show:true
        vnode.context.$refs[binding.value][0].show = true;
    },
    unbind() {
        document.body.removeEventListener('keyup', handleDialogClose);
    }
};
export default dialogShow;

I add my directive to vue like so,

import Vue from 'vue';

import dialogShow from '../../../common/directives/src/dialogOpen';

Vue.directive('dialog-show', dialogShow);

and attach to HTML element like this <button v-dialog-show="'dialog1'">Show Dialog 1</button>

This in turn should launch the ` component, but I am getting the following error,

TypeError: Cannot read property '0' of undefined

which is getting thrown at,

vnode.context.$refs[binding.value][0].show = true;

No my understanding is that if I have more than one and directive on a page I need to access the refs array via [0] is this not correct?

I am currently building the page like this,

<div class="item" v-for="(item, index) in items" :key="index">
      <button v-dialog-show="item.slug" type="button">{{ item.title }}</button>
      <Dialog :item="item" :ref="item.slug" />
 </div>
        
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!