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

123
Views
vue directives and methods

I a vue directive that opens a modal window,

const modalOpen: DirectiveOptions = {
  bind(el: HTMLElement, binding: DirectiveBinding, vnode: any) {
    el.addEventListener('click', () => {
        vnode.context.$refs[binding.value].show = true;
        document.body.style.overflowY = 'hidden';
    })
  },
};

This works perfectly, what I am now wanting to do is close the model from within the modal component. I feel like I have 2 choices here,

The modal component looks like this,

<div class="modal" :class="{ 'modal--show': show }">
<div class="modal__background"></div>
<div class="modal__content">
  <div class="modal__header">
    <button type="button">&times;</button>
  </div>
  <div class="modal__body">
    <slot></slot>
  </div>
</div>

and is used like this,

<Modal ref="modal-1">
  <p>Hello World, I am the modal 1</p>
</Modal>

What I am wanting to do is close the modal when the user clicks the .modal__background or clicks the button in .modal__header. I wanted to just add v-close-modal to each of those elements, but the directive cannot access the ref.

Is there a way to get the components ref? I have tried this.$refs in the mounted() hook but I just a get a prototype object back.

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!