<template> <view> <view v-for="(date, index) in weekDates" :key="index"> <text>{{ date.day }}</text> <!-- 渲染日期的日 --> <text>{{ getWeekday(date.dayOfWeek) }}</text> <!-- 渲染对应的星期几 --> </view> </view> </template> <script> export default { data() { return { weekDates: [] // 存储一周的日期和星期几 } }, methods: { getWeekday(dayOfWeek) { // 根据数字获取对应的星期几 const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; return weekdays[dayOfWeek]; } }, mounted() { // 获取当前日期 let currentDate = new Date(); // 获取当前星期几(0-6,0表示星期日) let currentDayOfWeek = currentDate.getDay(); // 获取一周的日期只要日和星期几 for (let i = 0; i < 7; i++) { let day = new Date(currentDate); day.setDate(currentDate.getDate() + i); let dayOfWeek = day.getDay(); let formattedDate = { day: day.getDate(), dayOfWeek: dayOfWeek }; this.weekDates.push(formattedDate); } } } </script> <style> /* 样式 */ </style>