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

105
Visualizações
Bind a JavaScript array to a Vue 3 component's v-for in template to update DOM

I've just started learning Vue 3 and I'm trying to create a Vue Component that is bound to a JavaScript array ** (defined and modified outside Vue)** and uses that array to render a v-for list of child Vue components.

In the code below, I have my "layer-container" Vue component that has a template that renders a list of "layer-item" subcomponents using data from a JavaScript array layersGlobal. If you run it, you can see that the items are rendered properly from the initial data in the array, but when the array is updated (after 1 second, via a push() call in setInterval()) the list is not rerendered, and the DOM is not updated.

<html>

<body>
  <div id="layers">
    <layer-container />
  </div>

  <script src="https://unpkg.com/vue@3"></script>
  <script>
    var layersGlobal = [
      {name: "Layer A"},
      {name: "Layer B"}
    ];

    let app = Vue.createApp({
      data: function() {
        return {}
      },
      methods: {}
    });

    app.component("layer-container", {
      template: `
                  <div class="layer-container text-light">
                      <layer-item v-for="layer in layers" v-bind:name="layer.name" v-bind:key="layer.name"></layer-item>
                  </div>
              `,
      components: ["layer-item"],
      data: function() {
        return {
          layers: layersGlobal
        }
      },
    });

    app.component("layer-item", {
      template: `
                  <div class="layer-item">
                      <span>{{ name }}</span>
                  </div>
              `,
      props: ["name"]
    });

    app.mount("#layers");

    setTimeout(() => {
      layersGlobal.push({name: "Layer C"});
      console.log("Layers:", layersGlobal);
    }, 1000);
  </script>
</body>

</html>

Am I doing something wrong, or misinterpreting how Vue is reactive with arrays? Is it possible to bind a JavaScript array declared outside Vue to a Vue component like this, or can/should I be doing it another way?

I've looked around for a solution and found some example code that works in Vue 2, but not Vue 3.

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

0

Reactive data must be updated within the context of the component. You probably want to call setTimeout inside of the component's definition and update the reactive variable this.layers instead of the global array.

app.component("layer-container", {
    /* ... */
    data: function () {
        return {
            layers: layersGlobal
        }
    },
    mounted() {
        setTimeout(() => this.layers.push({ name: "Layer C" }), 1000)
    }
})

Alternatively, if you need to update the data from outside the component, you need to explicitly define your global array as a reactive object, using the ref() function.

const layersGlobal = Vue.ref([ /* ... */ ])

setTimeout(() => layersGlobal.value.push({ name: "Layer C" }), 1000)

Note the .value property being used to access the actual object.

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