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

159
Visualizações
mounted script not loading in vuejs

I have a script that toggles darkmode component which I have added to my index.html file in my public folder. Decided to move it to my view's itself and render on mounted. Not sure if I am doing it correctly though since it does not change to darkmode once toggled.

Here is the initial script I added to index.html in public folder:

  const checkbox = document.querySelector('#checkbox');
  const html = document.querySelector('html');

  const toggleDarkMode = function () {
    checkbox.checked
      ? html.classList.add('dark')
      : html.classList.remove('dark');
  }

  toggleDarkMode();
  checkbox.addEventListener('click', toggleDarkMode);

Here I am moving it to my view and load once mounted:

<script>
import Darkmode from '../components/Darkmode.vue';
import Footer from '../components/Footer.vue';

export default {
  name: 'About',
  components: {
    Darkmode,
    Footer,
  },
  mounted() {
    const checkbox = document.querySelector('#checkbox');
    const html = document.querySelector('html');

    const toggleDarkMode = () => (
      checkbox.checked ? html.classList.add('dark') :
        html.classList.remove('dark')
    );
    toggleDarkMode();
  },
};
</script>

Just for reference here is the DarkMode.vue component which toggles the mode:

<template>
    <div class="rounded-lg flex bg-gray-600 items-center justify-center mx-auto absolute top-1 right-0 left-3/3 mt-2 mr-2 z-50">
        <span class="text-md font-extralight">
            <i class="fas fa-sun"></i>
        </span>
        <div>
            <input class="hidden" id="checkbox" name="" type="checkbox"/>
            <label class="cursor-pointer" for="checkbox">
                <div class="w-9 h-5 flex items-center bg-gray-300 rounded-full p2">
                    <div class="w-4 h-4 bg-white rounded-full shadow switch-ball"></div>
                </div>
            </label>
        </div>
        <span class="text-xs font-semibold">
            <i class="fas fa-moon"></i>
        </span>
    </div>
</template>

<script>

export default {
  name: 'Darkmode',
  props: {
    msg: String,
  },
};

</script>

Am I missing something or this suppose to work?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If, as I understand, you intend to trigger the dark mode when activating your checkbox, it doesn't make much sense for you to place your logic in a lifecycle event like mounted. You can try using v-model and watch:

<template>
    <div class="rounded-lg flex bg-gray-600 items-center justify-center mx-auto absolute top-1 right-0 left-3/3 mt-2 mr-2 z-50">
        <span class="text-md font-extralight">
            <i class="fas fa-sun"></i>
        </span>
        <div>
            <input v-model="checkboxOn" class="hidden" id="checkbox" name="" type="checkbox"/>
            <label class="cursor-pointer" for="checkbox">
                <div class="w-9 h-5 flex items-center bg-gray-300 rounded-full p2">
                    <div class="w-4 h-4 bg-white rounded-full shadow switch-ball"></div>
                </div>
            </label>
        </div>
        <span class="text-xs font-semibold">
            <i class="fas fa-moon"></i>
        </span>
    </div>
</template>
<script>
import Darkmode from '../components/Darkmode.vue';
import Footer from '../components/Footer.vue';

export default {
  name: 'About',
  components: {
    Darkmode,
    Footer,
  },
  data: () => ({
    checkboxOn: 0,
    }),
  watch:{
    toggleDarkMode(){
      // This isn't a 'vue-style' procedure. I think you should try to communicate with your darkmode.vue component using Vuex, event bus, props...
      this.checkboxOn ? document.querySelector('html').classList.add('dark') :
      document.querySelector('html').classList.remove('dark')
    }
  }
};
</script>
about 4 years ago · Juan Pablo Isaza Relatório
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