vue创建一个日历组件

首先,在Vue项目中创建一个日历组件所需要的Vue单文件组件(.vue文件)。

在template标签中,可以使用table和tr标签来创建一个简单的日历表格,使用v-for指令来循环渲染每一行日期。

在script标签中:

  1. 使用moment.js库来处理日期时间,这个库非常方便,可以安装moment.js并引入。
  2. 设置当前月份和年份以及当前日期,使用计算属性computed来根据当前月份和年份计算出日历中的日期和周几。
  3. 在methods中,封装一个方法handlePrevMonth和handleNextMonth来控制上一个月和下一个月的切换。

最后,在style标签中,可以设置样式来美化日历组件。

这样,就可以创建一个基本的Vue日历组件了。具体实现可以看下面的代码:

vue 复制代码
<template>
  <div class="calendar">
    <table>
      <thead>
        <tr>
          <th colspan="7">{{ currentMonth }} {{ currentYear }}</th>
        </tr>
        <tr>
          <th v-for="day in days">{{ day }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="week in weeks" :key="week">
          <td v-for="day in week" :class="{ 'today': isToday(day), 'different-month': !isCurrentMonth(day) }">{{ day.format('D') }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import moment from 'moment';

export default {
  name: 'Calendar',
  data() {
    return {
      currentDate: moment(),
    };
  },
  computed: {
    currentMonth() {
      return this.currentDate.format('MMMM');
    },
    currentYear() {
      return this.currentDate.format('YYYY');
    },
    days() {
      return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    },
    weeks() {
      const weeks = [];
      const date = this.currentDate.clone().startOf('month').startOf('week');
      do {
        const week = [];
        for (let i = 0; i < 7; i++) {
          week.push(date.clone());
          date.add(1, 'day');
        }
        weeks.push(week);
      } while (date.month() === this.currentDate.month());
      return weeks;
    },
  },
  methods: {
    isToday(date) {
      return moment().isSame(moment(date), 'day');
    },
    isCurrentMonth(date) {
      return moment(date).isSame(this.currentDate, 'month');
    },
    handlePrevMonth() {
      this.currentDate.subtract(1, 'month');
    },
    handleNextMonth() {
      this.currentDate.add(1, 'month');
    },
  },
};
</script>

<style scoped>
.calendar {
  margin: 20px;
}

table {
  border-collapse: collapse;
  width: 100%;
  max-width: 600px;
}

th,
td {
  text-align: center;
  padding: 10px;
  border: 1px solid #ddd;
}

.weekend {
  color: red;
}

.today {
  background-color: #f5f5f5;
}

.different-month {
  color: #ddd;
}
</style>

然后在页面中引入Calendar组件即可。

相关推荐
松树戈19 分钟前
openfeign与dubbo调用下载excel实践
vue.js·spring cloud·elementui·dubbo
码农黛兮_4621 分钟前
HTML、CSS 和 JavaScript 基础知识点
javascript·css·html
狂野小青年24 分钟前
npm 报错 gyp verb `which` failed Error: not found: python2 解决方案
前端·npm·node.js
鲁鲁51728 分钟前
Windows 环境下安装 Node 和 npm
前端·npm·node.js
跑调却靠谱1 小时前
elementUI调整滚动条高度后与固定列冲突问题解决
前端·vue.js·elementui
全职计算机毕业设计1 小时前
SpringBoot Vue MySQL酒店民宿预订系统源码(支付宝沙箱支付)+代码讲解视频
vue.js·spring boot·mysql
呵呵哒( ̄▽ ̄)"1 小时前
React - 编写选择礼物组件
前端·javascript·react.js
Coding的叶子1 小时前
React Flow 简介:构建交互式流程图的最佳工具
前端·react.js·流程图·fgai·react agent
apcipot_rain6 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
油丶酸萝卜别吃6 小时前
OpenLayers 精确经过三个点的曲线绘制
javascript