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

202
Visualizações
Mutation observer not observing element when placed inside a function

I'm trying to set the liveChatAvailable value to true and isLoading value to false once the cripClient element loads to the page.

When the observer object is within a function the if (crispClient) code never runs.

After some research, it seems that it might have something to do with the code needing to be asynchronous but I don't really know how to go about it so a push in the right direction would be great.

Update: I made the mixin run the code on mounted() instead of doing it inside of the component to see if that would make a difference but it didn't.

LiveChatAvailability.js

  export const LiveChatAvailability = {
    data() {
      return {
        isLoading: true,
        liveChatAvailable: false
      }
    },
  
    methods: {
      setLiveChatAvailability() {      
        
        const crispClient = document.querySelector('.crisp-client');
        const observer = new MutationObserver((mutations, obs) => {
          if (crispClient) {
  
            this.loading = false;
            this.liveChatAvailable = true;

            obs.disconnect();
            return;
          }
  
          observer.observe(document, {
            childList: true,
            subtree: true
          });
        });
      }
    }
  } 

LiveChatButton.vue

<template>
  <button v-if="liveChatAvailable" @click.prevent="liveChatTrigger">Start a live chat now</button>
</template>

<script>
import {LiveChatAvailability} from '../../../../../public_html/assets/src/js/Vue/Mixins/LiveChatAvailability';
export default {
  mixins: [
    LiveChatAvailability
  ],
  created() {
    this.setLiveChatAvailability();
  },
  methods: {
    liveChatTrigger() {
      if (window.$crisp) {
        window.$crisp.push(['do', 'chat:open']);
      }
    }
  },
}
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Placing the crispClient const within the MutationObserver code allowed it to work.

  export const LiveChatAvailability = {
    data() {
      return {
        isLoading: true,
        liveChatAvailable: false
      }
    },
  
    methods: {
      setLiveChatAvailability() {      
        
        
        const observer = new MutationObserver((mutations, obs) => {
          if (crispClient) {

            const crispClient = document.querySelector('.crisp-client');
  
            this.loading = false;
            this.liveChatAvailable = true;

            obs.disconnect();
            return;
          }
  
        });

          observer.observe(document, {
            childList: true,
            subtree: true
          });
      }
    }
  } 
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