【发布时间】:2018-12-12 11:50:35
【问题描述】:
我有一个array - 'items' 和objects,每个都包含一个array 和更多objects。
我想access 第二个数组'productPrices' 使用v-for。但是items.productPrices 对我不起作用。我应该以某种方式创建双循环吗?
HTML:
<table>
<tbody>
<tr v-for="(element, index) in items.productPrices">
<td>{{ element.name }}</td>
<td>
<span>{{ element.amount }}</span>
</td>
<td>
{{ element.price }}
</td>
</tr>
</tbody>
</table>
JS:
new Vue({
el: "#app",
data: {
items: [
{
name: 'menu',
path: 'menu',
productPrices: [
{ amount: 100, price: 80.61, name: 'first' },
{ amount: 250, price: 72.10 },
{ amount: 500, price: 79.62 },
{ amount: 1000, price: 100.20 },
{ amount: 2500, price: 147.60 },
{ amount: 5000, price: 232.56 }
],
quantity: 0
},
{
name: 'Etui',
path: 'etui',
productPrices: [
{ amount: 100, price: 80.61, name: 'first' },
{ amount: 250, price: 72.10 },
{ amount: 500, price: 79.62 },
{ amount: 1000, price: 100.20 },
{ amount: 2500, price: 147.60 },
{ amount: 5000, price: 232.56 }
],
quantity: 0
},
]
}
})
这是fiddle。
【问题讨论】:
-
items是一个您无法直接访问的数组items.productPrices。你必须提供像items[0].productPrices这样的索引,否则你必须通过循环。 -
我猜你确实需要使用两个 v-for 循环。您也可以先转换数据,然后使用 v-for,尽管我不会这样做。
标签: javascript arrays vue.js