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

143
Visualizações
Access dom element list of classes with Vue 3 composition API

I'm trying to watch if a class exist in a dom element inside my component but can't figure out the way to do this inside the onMounted hook:

This a simplified version of my template:

<template>
        <a
          ref="tabLink"
          href="#"
          role="tab"
          :class="{ chatUpdated: this.chatSessionId === tab.chatSessionId }"
        >
          {{ tab.title }}
        </a>
</template>

This is my setup function:

  setup() {
    const tabLink = ref(null);
    onMounted(() => {
      console.log(tabLink.value["0"].classList[0]);
    });
  } 

As you can see I'm trying to console.log the classes of my <a> tag in my template but I always get an undefined value. I thought that the component's DOM was already there when the onMounted hook is called so I don't get why I'm getting an undefined value.

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

0

The way you look for the class is wrong.

You need to:

const tabLink = ref(); // remove the null from here

console.log(tabLink.value.className); // gets all the classes as string
console.log(tabLink.value.className.split(" ")); // returns an array of classes
about 4 years ago · Juan Pablo Isaza Relatório

0

From the Vue docs:

When you mutate reactive state in Vue, the resulting DOM updates are not applied synchronously. Instead, Vue buffers them until the "next tick" to ensure that each component updates only once no matter how many state changes you have made.

In your case, there's a class binding on the <a> which changes based on chatSessionId. That binding's effect is seen in the next tick.

Solution

Await a call to nextTick() to observe the effect:

            👇
onMounted(async () => {
    👇
  await nextTick()

  // class list effect is now available
  console.log(tabLink.value[0].classList[0])
})

demo

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