搜尋

首頁  >  問答  >  主體

展示在過濾函數中使用的prop - axios, vue3

當使用axios取得資料時,如何在filteredProducts中顯示道具。我已經通過瞭如下圖的道具。

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,
    },
  },

我想用正在傳遞的品牌名稱來取代「Nike」。非常感謝

P粉439804514P粉439804514461 天前577

全部回覆(2)我來回復

  • P粉872101673

    P粉8721016732023-09-09 09:13:18

    選擇名為「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>

    回覆
    0
  • P粉529245050

    P粉5292450502023-09-09 09:09:06

    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,
                    },
                  },
              }

    回覆
    0
  • 取消回覆