我的自定义组件代码:
<template>
<div class="event">
<div class="title text">
{{ title }}
</div>
<div class="date text">
{{ date }}
</div>
<div class="time text">
{{ time }}
</div>
<div class="address text">
{{ address }}
</div>
<div class="learn-more text">
Learn more!
</div>
</div>
<script>
export default {
name: 'EventCard',
props: {
title: {
type: String,
required: true,
default: ''
},
date: {
type: String,
required: false,
default: ''
},
time: {
type: String,
required: true,
default: ''
},
address: {
type: String,
required: true,
default: ''
},
type: {
type: String,
required: true,
default: ''
}
}
}
还有些造型。
然后,我将在EventDiscoveryPage.vue文件中使用此自定义组件,其中的代码如下所示:
<div class="events">
<EventCard class="food-event" :title="'food event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="middle-row-event music-event" :title="'music event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="music-event" :title="'music event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="comedy-event" :title="'comedy event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="middle-row-event music-event" :title="'music event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="food-event" :title="'food event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="music-event" :title="'music event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="middle-row-event comedy-event" :title="'comedy event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
<EventCard class="comedy-event" :title="'comedy event'" :date="'date-test'" :time="'time-test'" :address="'address-test'" :type="'music'" />
</div>
到目前为止,代码按预期工作,但我想减少这些看似冗余的代码,并想知道我是否能够以某种方式使用v-for来自动化这个过程,但我不确定,因为我看到了v-for所需的预设数据?我无法使用文档来找出答案,不知道是否有人有任何建议。
2条答案
按热度按时间wn9m85ua1#
你可以使用v-来重构你的代码,以迭代一个对象数组,如下所示:
puruo6ea2#
您可以像这样将所有这些对象缩减为EventDiscoveryPage.vue中的一个对象,然后将这些对象添加到一个数组中并循环遍历该数组: