2 回答
TA贡献1856条经验 获得超5个赞
json直接分配给this.productslike :
fetch('https://fakestoreapi.com/products')
.then(response => response.json())
.then(json => {
this.products = json
})
因为json代表products列表,所以json.products会返回undefined
计算属性应定义如下:
computed:{
comp1(){...},
comp2(){...}
}
<div id="app">
<h2>Total inventory: {{ totalProducts }}</h2>
<ul>
<li>beginning of the list</li>
<li v-for="product in products">
{{ product.title }} {{ product.price }}
</li>
<li>End of the list</li>
</ul>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
const app = new Vue({
el: '#app',
data: {
products: []
},
created() {
fetch('https://fakestoreapi.com/products')
.then(response => response.json())
.then(prod => {
this.products = prod
})
},
computed: {
totalProducts() {
return this.products.reduce((sum, product) => {
return sum + product.price
}, 0)
}
}
})
</script>
TA贡献1852条经验 获得超7个赞
您的数据应该返回一个返回新对象的函数:
短途
data: () => ({
products: []
})
很长的路要走
data() {
return {
products: []
}
}
添加回答
举报