gpt4 book ai didi

javascript - 如何避免在使用 v-for 的元素上使用 v-if

转载 作者:搜寻专家 更新时间:2023-10-30 22:25:07 26 4
gpt4 key购买 nike

我有一个像这样的文字对象:

    export default {
pizze:{
type:"pizze",
typeOne:{
title: "Pizze Rosse",
data:[
{id:1,type:"pizza",name:"Margherita",price:4,ingredients:["pomodoro","mozzarella"],quantity:0,inventory:100},
{id:2,type:"pizza",name:"Marinara",price:3.5,ingredients:["aglio","pomodoro"],quantity:0,inventory:100},
{id:3,type:"pizza",name:"Salsiccia e funghi",price:6,ingredients:["salsiccia","funghi","mozzarella","pomodoro"],quantity:0,inventory:100},
{id:4,type:"pizza",name:"Carciofi",price:4,ingredients:["mozzarella","carciofi","pomodoro"],quantity:0,inventory:100},
]
},
typeTwo:{
title:"Pizze Bianche",
data:[
{id:5,type:"pizza",name:"Gorgonzola e noci",price:5.50,ingredients:["gorgonzola","noci","mozzarella"],quantity:0,inventory:100},
{id:6,type:"pizza",name:"Stracchino e rucola",price:4.50,ingredients:["stracchino","rucola","basilico"],quantity:0,inventory:100},
{id:7,type:"pizza",name:"Tartufo e salsiccia",price:8,ingredients:["tartufo","salsiccia","mozzarella"],quantity:0,inventory:100},
{id:8,type:"pizza",name:"Zucchine e Gamberi",price:5,ingredients:["zucchine","gamberi","pomodorini"],quantity:0,inventory:100},
]

}
},
primiPiatti:{
type:"primiPiatti",
typeOne:{
title: "Primi di carne",
data:[
{id:12,type:"primiPiatti",name:"Lasagne alla Bolognese",price:9,quantity:0,inventory:100},
{id:13,type:"primiPiatti",name:"Spaghetti alla carbonara",price:14,quantity:0,inventory:100},
{id:14,type:"primiPiatti",name:"pennette all'amatriciana",price:10,quantity:0,inventory:100},
{id:15,type:"primiPiatti",name:"paccheri salsiccia e funghi",price:12,quantity:0,inventory:100},
]
},
typeTwo:{
title:"Primi di pesce",
data:[
{id:16,type:"primiPiatti",name:"spaghetti allo scoglio",price:8.50,quantity:0,inventory:100},
{id:17,type:"primiPiatti",name:"zuppa di cozze",price:14.50,quantity:0,inventory:100},
{id:18,type:"primiPiatti",name:"pappardelle asparagi e gamberi",price:18,quantity:0,inventory:100},
{id:19,type:"primiPiatti",name:"lasagne al salmone",price:15,quantity:0,inventory:100},
]

},
...

我不知道使用 v-if inside a v-for loop 不好我在 View 中显示代码的第一种方法是:

<div class="menu__container">
<div v-if="menuVoice!=='type'"class="menu__container--box" v-for="menuVoice in Object.keys(menuData)" :key="menuVoice">
<div class="title">{{menuData[menuVoice].title}}</div>
<item-record v-for="data in menuData[menuVoice].data" :key="data.id" :data="data"></item-record>
</div>
</div>

我试图做的是避免显示与对象 MenuVoicetype 属性相关的 div block 。因为我需要它用于其他目的(例如检索正确的对象)。所以我以这种方式重构了代码:

<div class="menu__container">
<div :style="[menuVoice === 'type' ? {display:'none'} : {display:'block'}]" class="menu__container--box" v-for="menuVoice in Object.keys(menuData)" :key="menuVoice">
<div class="title">{{menuData[menuVoice].title}}</div>
<item-record v-for="data in menuData[menuVoice].data" :key="data.id" :data="data"></item-record>
</div>
</div>

我不确定是否可以,因为那里总有一个 if 语句。代码在这两种情况下都有效,但我想知道是否有更好的方法来做类似的事情。

我还尝试将 v-if 向下移动到子 div 上,如下所示:

<div class="menu__container">
<div"class="menu__container--box" v-for="menuVoice in Object.keys(menuData)" :key="menuVoice">
<div v-if="menuVoice !=='type'>
<div class="title">{{menuData[menuVoice].title}}</div>
<item-record v-for="data in menuData[menuVoice].data" :key="data.id" :data="data"></item-record>
</div>
</div>
</div>

但它不起作用,因为我仍然会得到三个类为 '.menu__container--box' 的 div。

最佳答案

避免将 v-ifv-for 一起使用的技巧是使用计算属性过滤数据集。

我在这里创建了一个名为 menuList 的计算,它过滤 menuData,返回过滤后的浅层克隆。

computed: {
menuList() {
return Object.keys(this.menuData)
.filter(key => key !== 'type')
.reduce((res, key) => (res[key] = this.menuData[key], res), {});
}
}

因此您的模板使用开箱即用的正确列表。 v-for="(item, key) in menuList"

<div class="menu__container">
<div class="menu__container--box" v-for="(item, key) in menuList" :key="key">
<div class="title">{{item.title}}</div>
<item-record v-for="data in item.data"
:key="data.id" :data="data"></item-record>
</div>
</div>

模板逻辑应保持在最低限度。任何超出对值的引用或简单赋值的内容都应移至计算和方法中。

关于javascript - 如何避免在使用 v-for 的元素上使用 v-if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49707180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com