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

303
Views
How to make a computed data reactive after having fetched data via VueX?

In a Vue.js application, I have fetched a product based on provided route id.

Product object in an array has pictures. I want to create a copy of reactive data "mosaicImagesList" that is reflected once the product is fetched, and it should have following structure:

[
    {
        "url": "https://via.placeholder.com/650x250",
        "isWide": true,
        "isTall": false
    },
    {
        "url": "https://via.placeholder.com/350x150",
        "isWide": true,
        "isTall": false
    }
]

It shows an output in console.log, but it shows an empty list of array in the rendered page.

Here is my code:

export default {
  name: 'products.show',

  data () {
    return {
      slide: 0
    }
  },

  computed: {
    product () {
      return this.$store.state.product
    },

    images () {
      const items = this.product.picture || []
      const result = []
      items.forEach(image => {
        const img = new Image()
        img.addEventListener('load', function () {
          const isWide = img.width > img.height
          const isTall = img.height > img.width
          result.push({ url: image, isWide, isTall })
        })
        img.src = image
      })
      console.log(result) // <--- THIS WORKS IN THE CONSOLE LOG DEV TOOL
      return result
    }
  },

  methods: {
    async fetchProduct () {
      await this.$store.dispatch('products/getById', this.$route.params.id)
    }
  },

  async created () {
    await this.fetchProduct()
  }
}

Here is the HTML section:

          <div class="image-mosaic">
            <div
              v-for="(image, index) in images"
              :key="index"
              :style="`background-image: url('${image.url}')`"
              class="card" />
          </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The for..in-loop iterates over the enumerable properties of an Object.
As images is an Array the solution might be to use a for..of-loop.

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!