Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!