How to loop items in Vuex store
P粉513318114
P粉513318114 2023-09-11 23:38:31
0
1
637

This is my store in Vue dev tools:

This is my Vue component:

<template>
  <div>
      <div
        v-for="product in allProducts"
        :key="product._id"
      >
        {{ product.brand }}
      </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return{
      allProducts:[],
    }
  },
  computed: {
    ...mapGetters(["allProducts"])
  },
  mounted() {
    this.$store.dispatch("getProducts");
  }
};
</script>

If I use this:

{{allProducts}}

I got all the products.

But when trying to use this loop:

<div
        v-for="product in allProducts"
        :key="product._id"
      >
        {{ product.brand }}
      </div>

Do not show.

Can you tell me what to do?

P粉513318114
P粉513318114

reply all(1)
P粉536909186

allProducts is an object with property products, so in order to loop over products try product in allProducts.products

const app = Vue.createApp({
  data() {
    return {
      allProducts: {
        products: [
          {brand: 'aaa'}, {brand: 'bbb'}
        ]
      }
    };
  },
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <div v-for="product in allProducts.products">
    {{ product.brand }}
  </div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template