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

157
Views
Directiva Vue para abrir un modal

Quiero crear una directiva vue que se pueda agregar a cualquier elemento HTML en el que hacer clic adjuntaría un evento de clic para abrir un componente modal "relacionado".

Mi directiva se ve así,

 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;

Agrego mi directiva a vue así,

 import Vue from 'vue'; import dialogShow from '../../../common/directives/src/dialogOpen'; Vue.directive('dialog-show', dialogShow);

y adjunte a un elemento HTML como este <button v-dialog-show="'dialog1'">Show Dialog 1</button>

Esto, a su vez, debería iniciar el componente `, pero recibo el siguiente error:

TypeError: no se puede leer la propiedad '0' de indefinido

al que se le tira,

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

No, tengo entendido que si tengo más de una directiva en una página, necesito acceder a la matriz de referencias a través de [0] , ¿no es correcto?

Actualmente estoy construyendo la página así,

 <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!