用vue写一个日历组件
时间: 2023-12-28 08:19:15 浏览: 62
使用vue写的一个简单的日历
好的,我可以为您提供一些Vue.js的日历组件代码示例:
```
<template>
<div>
<h2>Calendar</h2>
<table>
<thead>
<tr>
<th v-for="day in daysOfWeek">{{ day }}</th>
</tr>
</thead>
<tbody>
<tr v-for="week in weeks">
<td v-for="day in week">
<div v-if="day">
<span>{{ day.date }}</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
daysOfWeek: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
today: new Date(),
currentMonth: null,
currentYear: null,
weeks: [],
};
},
mounted() {
this.setCurrentDate();
this.createCalendar();
},
methods: {
setCurrentDate() {
this.currentMonth = this.today.getMonth();
this.currentYear = this.today.getFullYear();
},
createCalendar() {
const weeks = [];
let firstDayOfMonth = new Date(this.currentYear, this.currentMonth, 1).getDay();
let lastDateOfMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
let day = 1;
for (let i = 0; i < 6; i++) {
const week = [];
for (let j = 0; j < 7; j++) {
if (i === 0 && j < firstDayOfMonth) {
week.push(null);
} else if (day > lastDateOfMonth) {
break;
} else {
week.push({ date: day });
day++;
}
}
weeks.push(week);
if (day > lastDateOfMonth) {
break;
}
}
this.weeks = weeks;
},
},
};
</script>
```
这个日历组件可以显示当前月份的日历,以周日为每周的第一天。您可以根据自己的需求进行修改。
阅读全文