当前位置:首页 > VUE

vue实现事件日历

2026-02-10 20:21:28VUE

实现事件日历的基本思路

使用Vue实现事件日历需要结合日期处理库(如date-fnsdayjs)和组件化开发。核心功能包括:日期渲染、事件管理、交互逻辑(如点击添加事件)。

安装依赖

推荐使用dayjs处理日期,轻量且API友好:

vue实现事件日历

npm install dayjs

基础日历组件结构

创建Calendar.vue文件,定义月份切换和日期网格:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h3>{{ currentMonth }}</h3>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="days-grid">
      <div v-for="day in days" :key="day.date" class="day-cell">
        {{ day.date }}
        <div v-for="event in day.events" :key="event.id">{{ event.title }}</div>
      </div>
    </div>
  </div>
</template>

日期数据处理

script部分实现月份切换和日期生成逻辑:

vue实现事件日历

import dayjs from 'dayjs';

export default {
  data() {
    return {
      currentDate: dayjs(),
      events: [
        { id: 1, title: '会议', date: '2023-10-15' },
        { id: 2, title: '生日', date: '2023-10-20' }
      ]
    };
  },
  computed: {
    currentMonth() {
      return this.currentDate.format('YYYY年MM月');
    },
    days() {
      const startOfMonth = this.currentDate.startOf('month');
      const endOfMonth = this.currentDate.endOf('month');
      const days = [];

      // 填充当月日期
      let date = startOfMonth;
      while (date.isBefore(endOfMonth) || date.isSame(endOfMonth)) {
        days.push({
          date: date.date(),
          events: this.events.filter(e => dayjs(e.date).isSame(date, 'day'))
        });
        date = date.add(1, 'day');
      }

      return days;
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = this.currentDate.subtract(1, 'month');
    },
    nextMonth() {
      this.currentDate = this.currentDate.add(1, 'month');
    }
  }
};

添加事件交互

扩展组件以支持添加事件:

<template>
  <div class="day-cell" @click="openAddEvent(day)">
    <!-- 原有内容 -->
    <div v-if="selectedDay === day.date" class="event-form">
      <input v-model="newEventTitle" placeholder="事件标题">
      <button @click.stop="addEvent">添加</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDay: null,
      newEventTitle: ''
    };
  },
  methods: {
    openAddEvent(day) {
      this.selectedDay = day.date;
    },
    addEvent() {
      if (this.newEventTitle) {
        this.events.push({
          id: Date.now(),
          title: this.newEventTitle,
          date: this.currentDate.set('date', this.selectedDay).format('YYYY-MM-DD')
        });
        this.selectedDay = null;
        this.newEventTitle = '';
      }
    }
  }
};
</script>

样式优化

添加基础样式确保日历可读性:

.calendar {
  font-family: Arial;
  width: 100%;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.days-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
}
.day-cell {
  min-height: 80px;
  border: 1px solid #eee;
  padding: 4px;
}
.day-cell:hover {
  background-color: #f5f5f5;
}
.event-form {
  margin-top: 4px;
}

进阶功能建议

  • 拖拽调整事件:使用vuedraggable库实现事件拖拽
  • 周视图切换:通过计算属性生成周数据
  • 持久化存储:结合localStorage或后端API保存事件
  • 国际化:利用dayjs的locale功能支持多语言

完整实现可根据实际需求调整日期生成算法和交互细节。对于复杂场景,可考虑现成库如fullcalendar/vuev-calendar

标签: 日历事件
分享给朋友:

相关文章

jquery事件绑定

jquery事件绑定

jQuery 事件绑定的基本方法 使用 on() 方法绑定事件是最推荐的方式,兼容性强且支持动态元素。语法如下: $(selector).on(eventName, handlerFunction)…

vue实现change事件

vue实现change事件

Vue 实现 change 事件的方法 在 Vue 中,可以通过多种方式监听和处理 change 事件,以下是常见的实现方法: 使用 v-on 或 @ 语法监听 change 事件 <tem…

vue表格实现日历

vue表格实现日历

Vue 表格实现日历的方法 基础表格结构 使用 el-table 或原生 HTML 表格,通过 v-for 循环生成日历格子。月份天数通过 new Date() 计算,动态渲染表格内容。 <t…

vue 实现拖拽事件

vue 实现拖拽事件

实现拖拽事件的基本步骤 在Vue中实现拖拽功能可以通过HTML5的Drag and Drop API或第三方库如vuedraggable来完成。以下是两种方法的详细说明。 使用HTML5 Drag…

vue 日历组件实现

vue 日历组件实现

Vue 日历组件实现 基本结构搭建 使用Vue的单文件组件(SFC)结构,创建一个基础的日历组件框架。需要定义月份、年份的状态管理,以及日期渲染逻辑。 <template> <…

Vue实现日历日程

Vue实现日历日程

Vue 实现日历日程 使用第三方库(如 FullCalendar) FullCalendar 是一个功能强大的日历库,支持 Vue 集成。安装依赖: npm install @fullcalenda…