【发布时间】:2018-07-25 05:54:45
【问题描述】:
我有两个组件,CartOverview 和 CartCheckout。 CartOverview 是 CartCheckout 的父级。
我将一个数组传递给子组件,如下所示:
<cart-checkout :items="items"></cart-checkout>
在子组件中,我已经声明了道具:
props:['items']
每当我尝试访问道具时,都不会返回任何内容。我已经使用 Vue DevTools 来查看发生了什么,令人困惑的部分是 I can quite clearly see that the data is being passed down correctly.
当 parent 被挂载时,items 数组是这样填充的:
fetchCartItems() {
axios.get('/cart-items')
.then((response) => {
this.items = response.data;
this.total = this.getCartTotal();
})
.catch((error) => {
console.log(error);
});
},
为了澄清,我一直在尝试从道具中获取数据,如下所示:
this.items
但就像我说的,nothing is returned.
getCartTotalRaw() {
let total = 0;
alert(this.items);
this.items.forEach(function(item) {
total += (Number(item.product.price) * item.quantity);
});
return total;
}
我在这里错过了什么?
【问题讨论】:
-
getCartTotalRaw是计算函数还是普通方法? -
您发布的部分似乎没问题。那么您可以发布更多代码还是使用Vue Devtool之类的工具来跟踪数据和道具?
-
@Ferrybig 一个正常的方法。
-
@obfish 这是我能想到的唯一相关代码。我已经看到使用 Vue Devtools 将数据注册在道具本身中,但它只是访问导致问题的道具。
-
完全空白的警报可能意味着 this.items=[]。是 this.items = [] 的初始值吗?