【发布时间】:2021-05-31 05:53:09
【问题描述】:
我正在使用 v-for 使用 Vue 渲染 JSON 列表中的所有内容。我有以下结构的 JSON 文件:
{
"Cards": [
{
"id": 0,
"name": "TITLE",
"description": "Lorem ipsum",
"price": "2500",
"services": [
{
"icons": [
{
"pre": "fas",
"icon": "male",
"tooltip": "Maximum number of people",
"after": "2"
},
{
"pre": "fas",
"icon": "bed",
"tooltip": "Number of beds",
"after": "2 + (side bed)"
}
]
},
{
"icons": [
{
"pre": "fas",
"icon": "coffee",
"tooltip": "Services",
"after": "TV, Wi-Fi, parking"
}
]
}
]
},
{
"id": 1,
"name": "TITLE",
"description": "Lorem ipsum",
"price": "3200",
"services": [
{
"icons": [
{
"pre": "fas",
"icon": "female",
"tooltip": "Maximum number of people",
"after": "2"
}
]
}
]
}
]
}
这是我当前的渲染结构:
<div v-for="(card, index) in Cards" :key="index">
<div>
<template v-for="(icons, icon_ids) in card.services" :key="icon_ids">
<div>
<template v-for="(icon, icon_idx) in icons.icons" :key="icon_idx">
<div>
<font-awesome-icon :icon="[icon.pre, icon.icon]"/>
<span>{{icon.after}}
<sup v-if="icon.sup != null">{{icon.sup}}</sup>
</span>
<span class="tip-inner">{{ icon.tooltip }}</span>
</div>
</template>
</div>
</template>
</div>
</div>
编辑 1:
通过这种方式,我所有来自services 的icons 在每张卡片中呈现两次。第一张卡上应该只有man、bed、coffee,另一张卡上应该是woman,但是......所有这些都在两张卡上。
所以Cards 和id: 0 将有两个图标,而id: 1 将只有一个图标。
我怎样才能让services 只为他们所在的卡渲染?
编辑 2:(解决方案)
一切都像这样完美地工作......我只是犯了一个错误,并且在造成我错误的组件上方有另一个v-for。我在这里发布的这段代码是正确的。
【问题讨论】:
标签: javascript json vuejs3