Учитывая <p>{{showOpeningHours}}</p>
, вычисляемое свойство showOpeningHours
может форматировать данные о часах работы в одну длинную строку:
get showOpeningHours() {
const daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
const openingHours =
this.foodMerchant.opening_hours.data
.map(hours => {
const timeStr = DateTime.fromObject({
hour: hours.startHour,
minute: hours.startMinute,
zone: this.foodMerchant.timeZone
}).toFormat('h:mm a');
return daysOfWeek[hours.day] + ' ' + timeStr;
});
return openingHours.join(' / ');
}
demo 1
Или выможет изменить вычисляемое свойство в массив, отображаемый с помощью v-for
:
get showOpeningHours() {
const daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
const openingHours =
this.foodMerchant.opening_hours.data
.map(hours => {
const timeStr = DateTime.fromObject({
hour: hours.startHour,
minute: hours.startMinute,
zone: this.foodMerchant.timeZone
}).toFormat('h:mm a');
return daysOfWeek[hours.day] + ' ' + timeStr;
});
return openingHours; // <-- RETURNS ARRAY
}
шаблон:
<ul>
<li v-for="hours in showOpeningHours" key="hours.day">{{hours}}</li>
</ul>
демо 2