Vue.js 如何使用v-for仅循环一次重复数据?
我正在从云端接收数据。我看过StackOverflow中的许多问题,但所有问题都使用了Vue.js 如何使用v-for仅循环一次重复数据?,vue.js,v-for,Vue.js,V For,我正在从云端接收数据。我看过StackOverflow中的许多问题,但所有问题都使用了data()中的数据。我是Vue.js的超级初学者,因此无法在我的代码中应用它 我只想在v-for中显示重复数据一次。在本例中,{{item.matched_product.name}重复显示 <template slot-scope="scope"> <div v-if="scope.row.external_store.service_name == 's
data()
中的数据。我是Vue.js的超级初学者,因此无法在我的代码中应用它
我只想在v-for
中显示重复数据一次。在本例中,{{item.matched_product.name}
重复显示
<template slot-scope="scope">
<div v-if="scope.row.external_store.service_name == 'sellorder'">
<div
v-for="item in scope.row.items"
:key="item.id"
class="option-detail"
ref="option_variant"
>
<div v-if="item.matched_product">
<-- This is the part I want to display only once -->
<span style="font-weight:bold">
<i class="el-icon-caret-right"></i>
{{item.matched_product.name}}
</span>
<span>
<span
v-if="item.matched_variant">
{{item.matched_variant.options.map(obj => obj.value).join(' / ')}} {{item.qty}} / {{item.matched_variant.properties && item.matched_variant.properties.soldout ?
'(sold out)':''}}
</span>
<span v-else>No matching</span>
<el-button size="mini" round @click="matchProduct(scope.row,item)">Edit matching</el-button>
</span>
</div>
<div v-else>
<el-button size="mini" @click="matchProduct(scope.row,item)">Match</el-button>
</div>
</div>
</div>
</template>
{{item.matched_product.name}
{{item.matched_variant.options.map(obj=>obj.value).join('/')}{{item.qty}/{{item.matched_variant.properties&&item.matched_variant.properties.soldout?
“(售罄)”:”}
不匹配
编辑匹配
匹配
我该怎么办
您可以通过执行以下操作访问
for循环中的索引
v-for=“(项目,索引)在scope.row.items中”
然后,您只需将v-if
添加到只想显示一次的元素中,即可检查如果索引为0
我在下面添加了@JoshBonnick-mentinoed
v-for="(item, index) in scope.row.items"
我也写了这个代码
{{ ((index == 0) || item.matched_product.name != scope.row.items[index-1].matched_product.name) ? item.matched_product.name : '' }}
现在,这很好。谢谢你的回答。我做了你提到的,并在scope.row.items“
中添加了v-for=“(项,索引)”,但我有另一个问题,它没有在同一元素中显示另一个匹配的\u product.name
。你现在有这个问题了吗{item.matched_product.name}
是的,我是这样写的,我把v-for=“(item,index)放在scope.row.items”
的第4行,而现在,我只有一个matched_product.name
,它会不时有不同的名称。我刚加了照片!数组scope.row.items
将接收许多不同的matched_product.name
{((index==0)| item.matched_product.name!=scope.row.items[index-1].matched_product.name)?item.matched_product.name:'}
我这样做了,效果很好!!无论如何,谢谢你的回答!!