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

118
Views
Javascript method document.querySelector alternative in VueJs for optimization

I need to optimize my code with all features of VueJs, so i've changed all the document.getElementById and document.querySelectorAll by using the ref in VueJs

So the only thing that i dont found an alternative is how to change : document.querySelector with another code related to VueJs :

document.querySelector('.mc-layer__content').classList.add('disable-scroll')

Is there any optimization related to this ?

Another important point, i can't use the ref here because the class is in the parent no in the component, so when i use this.$refs i can't see the parent element.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The solution that i've foud is to use the store because the parent isnt direct :

In the child component instead to use document.querySelector,

document.querySelector('.mc-layer__content').classList.remove('disable-scroll')

i've used :

this.$store.dispatch('app/disableScroll')

And in the parent, i've add the dynamic class :

:class="{ 'disable-scroll': isDisabled }"

The variable isDisabled has been managed in the store with disableScroll variable like this:

disableScroll(state) {
      state.disableScroll = true
    },
    enableScroll(state) {
      state.disableScroll = false
    },
about 4 years ago · Juan Pablo Isaza Report

0

Try with class binding:

new Vue({
  el: "#demo",
  data () {
    return {
      scroll: true
    }
  }
})
.disable-scroll {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <button @click="scroll = !scroll">switch</button>
  <div class="mc-layer__content" :class="{'disable-scroll': scroll}">
    ssssss
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
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!