代码优化 — 智慧饭堂小程序订餐详情页
修改之前数据结构格式处理:
修改之前数据结构格式显示:
修改之前页面显示处理:
修改之后数据结构格式处理:
修改之后数据结构格式显示:
修改之后页面显示处理:
修改之后页面代码:
<template>
<view class="pi-pd-top-16">
<view v-for="dateMeal in menuListDto" :key="dateMeal.date" class="pi-mg-bottom-16">
<pi-card padding="24rpx 24rpx 0">
<template slot="body">
<view
class="date pi-h-56 pi-bg-primary pi-radius-8 pi-flex-row-center pi-white pi-mg-bottom-32 pi-mg-top-18"
style="width: 160rpx;"
>
{{ $dayjs(dateMeal.date).format('MM.DD') }}
<text class="pi-mg-left-8">{{ $dayjs(dateMeal.date).format('ddd') }}</text>
</view>
<view v-for="meal in dateMeal.meal" :key="meal.id" class="pi-flex">
<view
v-for="mealTime in dateMeal.mealTime"
:key="mealTime.key"
class="meal-time pi-flex"
:class="[mealTime.key === meal.mealTime ? 'pi-mg-right-24' : '']"
>
<template v-if="mealTime.key === meal.mealTime">
<view
class="pi-w-12 pi-h-12 pi-radius-round pi-mg-right-16 pi-mg-top-8"
:style="[{ background: mealTime.color }]"
/>
{{ mealTime.name }}
</template>
</view>
<view class="menu-package pi-flex-sub">
<view class="pi-fz-28 pi-fw-600 pi-mg-bottom-16 pi-flex-sub">
{{ meal.id ? meal.menuName : '此餐不选' }}
</view>
<view class="pi-light-gray pi-fz-24 pi-mg-bottom-32" style="line-height: 1.5;">
<view style="line-height: 1.3;">
{{ meal.id ? meal.packageName : '' }}
</view>
</view>
</view>
<view class="price pi-text-right" style="width: 104rpx;">
{{ (meal.id ? meal.price : 0) | moneyFormat }}
</view>
</view>
</template>
</pi-card>
</view>
</view>
</template>
<script>
const TAG_NAME = 'PackageDetail'
import groupBy from 'lodash/groupBy'
export default {
name: TAG_NAME,
props: {
daysInfo: {
default() {
return []
}
}
},
computed: {
menuListDto() {
const groupByDate = groupBy(this.daysInfo, meal => meal.date)
const menuListDto = Object.keys(groupByDate)
.map(date => ({
date,
menuList: groupBy(groupByDate[date], meal => meal.mealTime)
}))
.map(date => ({
...date,
meal: Object.values(date.menuList).flat(),
mealTime: Object.keys(date.menuList).map(mealTime => this.$consts.MEAL_TIMES[mealTime])
}))
return menuListDto
}
}
}
</script>
总结:
1、需要先分析好页面需要呈现的数据结构格式,然后按照格式对已有数据进行组装,最终将组合好的格式
在页面进行显示
2、将页面多余的代码删除
3、style样式绑定使用数组形式,兼容微信小程序
4、需要加强熟悉页面的作用和页面布局和数据处理