How to display props in filteredProducts when using axios to get data. I have passed the props as shown below.
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, }, },
I want to replace "Nike" with the brand name being passed. Thank you so much
P粉8721016732023-09-09 09:13:18
Select the product named "Nike" and pass it into filteredProducts
. Afterwards you can change the name using a computed property and use it in the template.
<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粉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, }, }, }