[英]Render value from List only once Vue 3
I am using v-for
to render everything from JSON List with Vue.我正在使用
v-for
来渲染 JSON List 和 Vue 中的所有内容。 I have JSON file of following structure:我有以下结构的 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>
EDIT 1: By this all my icons
from services
renderes twice in each card.编辑 1:由此,我所有来自
services
的icons
在每张卡中呈现两次。 On first card should be only man
, bed
, coffee
and on the other card should be woman
but... All of the are at both cards.第一张卡片上应该只有
man
、 bed
、 coffee
,另一张卡片上应该是woman
,但是……所有这些都在两张卡片上。
So Cards
with id: 0
would have two icons and id: 1
would have only one icon.所以
id: 0
的Cards
有两个图标,而id: 1
的卡片只有一个图标。 How can I make to render services
only for the card they are under?我怎样才能只为他们所使用的卡提供
services
?
EDIT 2: ( Solution ) Everything works perfectly like this... I just made mistake and had another v-for
above the component which created my mistake.编辑2:(解决方案)一切都像这样完美地工作......我只是犯了错误,并且在造成我错误的组件上方有另一个
v-for
。 This code I posted here is correct.我在这里发布的这段代码是正确的。
Ahoj Šimon,阿霍伊·西蒙,
Can you clarify what the issue is?你能澄清一下问题是什么吗? Seems to be working ok (although I used
fontawesome
without vue-fontawesome
, maybe :icon="[icon.pre, icon.icon]"
is giving you unexpected results.)似乎工作正常(虽然我在没有
vue-fontawesome
fontawesome
下使用了 fontawesome ,但也许:icon="[icon.pre, icon.icon]"
会给你带来意想不到的结果。)
const 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" } ] } ] } ]; Vue.createApp({ setup() { return { Cards } } }).mount("#app");
.card{display:block; border: 1px solid black;padding:8px;margin:8px;border-radius:4px;}.icon{font-size: 1em; color: Tomato; min-width:30px;display:inline-block;text-align:center;}.tip-inner{color:#aaa;}
<script src="https://unpkg.com/vue@3.0.5/dist/vue.global.prod.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"/> <div id="app"> <div v-for="(card, index) in Cards":key="index" class="card"> <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> <span class="icon"><i:class="icon.pre + ' fa-' + icon.icon"></i></span> <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> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.