我有这个巨大的烦人组件,需要在父模板中重复多次,因为父模板使用的是 v-if。这是组件代码:
<SelectCard
v-for="(channel, index) in category.visibleChannels"
:key="index + '-' + channel.id"
:channel="channel"
:channel-selected="isSelected(channel.id)"
:read-more-details="channelInfoDetails"
@select="onAddChannel"
@deselect="onRemoveChannel"
@read-more-changed="setChannelInfoDetails"
/>
每次渲染模板时唯一变化的是我循环的数组......这是问题的简化版本:
<template>
<div
ref="channels"
class="channels"
>
<div v-if="showCategories">
<div
v-for="category in sliderCategories"
:key="category.name"
>
<h3 v-text="category.name" />
<div
v-if="category.showAll"
class="channel-list show-all"
:class="channelListSize"
>
<ul>
<SelectCard looping over category.contents />
</ul>
</div>
<ChannelSlider
v-else
:category="category"
@visible-updated="setVisibleChannels"
>
<SelectCard looping over category.visibleChannels />
</ChannelSlider>
<div class="show-all-link">
<a
:class="category.showAll?'arrow-up':'arrow-down'"
class="link"
@keyup.enter="toggleShowAll(category.name, !category.showAll)"
@click="toggleShowAll(category.name, !category.showAll)"
v-text="showAllText(category.showAll)"
/>
</div>
</div>
</div>
<div v-else>
<div v-if="showNoSearchResult">
<SomeComponent with some props/>
</div>
<div :class="channelListSize" class="channel-list">
<ul>
<SelectCard looping over updatedChannels />
</ul>
</div>
</div>
<div
ref="someref"
class="someClass"
:style="{top: channelInfoDetails.top + 'px', position: 'absolute'}"
>
<AnotherComponent with some props/>
</div>
</div>
</template>
所以我的模板变得巨大,因为 SelectCard 代码有很多道具。
有没有办法可以将 SelectCard 放入父代码中的方法中,这样我就可以使用要使用的数组或其他方法调用函数?还是有其他我不知道的解决方案?
FFIVE
胡说叔叔
相关分类