In Filterfunktionen verwendete Requisiten anzeigen – Axios, Vue3
P粉439804514
P粉439804514 2023-09-08 10:56:43
0
2
574

So zeigen Sie Requisiten in gefilterten Produkten an, wenn Sie Axios zum Abrufen von Daten verwenden. Ich habe die Requisiten wie unten gezeigt übergeben.

export default {
  data() {
    return {
      filteredProducts: [],
    };
  },
  mounted() {
 axios
 .get('/src/stores/sneakers.json')
 .then(response => { const products = response.data
  this.filteredProducts = products.filter(product => product.name.includes('Nike'))
})
  },
  computed: {
        resultCount () {
            return Object.keys(this.filteredProducts).length 
        },
    },
  props: {
    brand: {
      type: Object,
    },
  },

Ich möchte „Nike“ durch den weitergegebenen Markennamen ersetzen. Vielen Dank

P粉439804514
P粉439804514

Antworte allen(2)
P粉872101673

选择名为“Nike”的产品并将其传递到 filteredProducts 中。之后,您可以使用计算属性更改名称并在模板中使用它。

<template>
  <div 
    v-for="(item, i) in nikeProducts"
    :key="i"
  >
  </div>
</template>

<script>
export default {
  props: {
    brand: {
      type: Object
    }
  }
  data() {
    return {
      filteredProducts: [],
    };
  },
  mounted() {
    axios
      .get('/src/stores/sneakers.json')
      .then(response => { 
          const products = response.data
          this.filteredProducts = products.filter(product => product.name.include('Nike'));
      })
  },
  computed: {
    nikeProducts() {
      return this.filteredProducts.map(product => product.name = this.brand.name)
    }
  }
}
</script>
P粉529245050
export default {
              data() {
                return {
                  products: [],
                };
              },
              mounted() {
                   this.getSneakers();
              },
              methods: {
                 getSneakers() {
                 axios
                  .get('/src/stores/sneakers.json')
                   .then(response => { 
                  this.products = response.data ?? [];
                      })
                  }
               },
              computed: {
                    resultCount () {
                        return this.filteredProducts?.length ?? 0
                    },
                    brandName() {
                        return this.brand?.name ?? "";
                    },
                    filteredProducts () {
                        return this.products?.filter(
        product => product.name?.toLowerCase()?.includes(this.brandName.toLowerCase())
                   )
                    },
                },
              props: {
                brand: {
                  type: Object,
                },
              },
          }
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage