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

565
Visualizações
Why does Node.childNodes not return all nodes in vuejs single file component?

From MDN

The Node.childNodes read-only property returns a live NodeList of child nodes of the given element where the first child node is assigned index 0. Child nodes include elements, text and comments.

Which works as expected

Jsfiddle

btn.addEventListener('click', ()=>{
    console.log(container.childNodes)
})
  <div id="container">
    <span>A</span>
    <span>B</span>
    <span>C</span>
  </div>
  <button id='btn'>Log Nodes</button>

0: "#text"
1: <span>
2: "#text"
3: ...
length: 7

This logs every node including text nodes, etc.

But when used in Vue it only logs elements without the text nodes inside.

1: <span>
2: <span>
3: <span>
length: 3

Codesandbox with vue

I know you're not suppose to access elements like this in Vue, but I'm wondering if this is normal behavior and why it happens. And how do I get all the nodes inside an element in Vue ?

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

0

It is very tricky to compare normal static HTML and what Vue renders because it's "apples vs pears" comparison.

The template you give to a Vue may look like HTML but Vue don't use it as HTML text. It parses the template and produce render function which is just JavaScript code, that generates VNode's (virtual DOM)

And the thing is that during the parsing, Vue tends to ignore all insignificant whitespace by default.

There is a compiler setting that controls the whitespace handling. While default value condense removes most of the whitespace, value preserve will preserve some.

You can compare the difference in online Vue template explorer:

  1. condense
  2. preserve
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