Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda