在您提供的代码中,您使用 Vue.js 的 v-for
指令来遍历 dutyList
数组,并动态生成多个视图。为了确保您能够打印 isToday(one.date)
的返回值并查看每个日期是否是今天,您可以在组件的某个生命周期钩子中(例如 mounted
)或在某个方法中执行打印操作。
下面是如何实现这一点的完整示例:
<template>
<view v-for="(one, index) in dutyList" :key="index" class="page">
<view
class="list-line background"
:class="{ 'highlight': isToday(one.date) }"
>
<text class="list-left">日期:</text>
<text class="list-right" style="margin-left: 75rpx;">{{ one.date }}</text>
</view>
<view
class="list-line"
:class="{ 'highlight': isToday(one.date) }"
>
<text class="list-left">白班人员:</text>
<text class="list-right">{{ one.name_white }}</text>
</view>
<view
class="list-line"
@tap="telPhoneWhite(one.tel_white)"
:class="{ 'highlight': isToday(one.date) }"
>
<text class="list-left">电话:</text>
<text class="list-right" style="margin-left: 75rpx;">{{ one.tel_white }}</text>
</view>
<view
class="list-line"
:class="{ 'highlight': isToday(one.date) }"
>
<text class="list-left">夜班人员:</text>
<text class="list-right">{{ one.name_black }}</text>
</view>
<view
class="list-line"
@tap="telPhoneBlack(one.tel_black)"
:class="{ 'highlight': isToday(one.date) }"
>
<text class="list-left">电话:</text>
<text class="list-right" style="margin-left: 75rpx;">{{ one.tel_black }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
// 假设你的数据源
dutyList: [
{ date: '2023-10-24', name_white: '张三', tel_white: '12345678901', name_black: '李四', tel_black: '09876543210' },
// 添加更多数据...
],
// ...
};
},
methods: {
isToday(date) {
const today = new Date();
const inputDate = new Date(date);
return (
today.getFullYear() === inputDate.getFullYear() &&
today.getMonth() === inputDate.getMonth() &&
today.getDate() === inputDate.getDate()
);
},
telPhoneWhite(tel) {
console.log(`拨打白班电话: ${tel}`);
},
telPhoneBlack(tel) {
console.log(`拨打夜班电话: ${tel}`);
},
logIsTodayValues() {
this.dutyList.forEach(item => {
console.log(`isToday(${item.date}): ${this.isToday(item.date)}`);
});
}
},
mounted() {
this.logIsTodayValues(); // 在组件挂载后打印所有日期的 isToday 返回值
}
};
</script>
<style scoped>
.highlight {
background-color: yellow; /* 根据需要调整高亮样式 */
}
.page {
margin-bottom: 20px; /* 根据需要调整页面间距 */
}
.list-line {
display: flex;
justify-content: space-between;
}
</style>
关键点说明:
isToday()
方法:判断给定日期是否为今天。logIsTodayValues()
方法:循环遍历dutyList
并输出每个日期的isToday
值。- 调用
logIsTodayValues()
:在组件挂载时自动执行打印,以便您能看到每个日期是否是今天。
根据您的需求,可以添加更多功能或样式,使得应用更加完善。如果有任何特定需求或问题,请告诉我!
内容由零声教学AI助手提供,问题来源于学员提问