Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

304
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda