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粉5369091862023-09-12 13:12:45
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>