当前位置:首页 > VUE

vue实现事件日历

2026-02-10 20:21:28VUE

实现事件日历的基本思路

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

安装依赖

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

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部分实现月份切换和日期生成逻辑:

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

vue实现事件日历

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

相关文章

实现Vue事件总线

实现Vue事件总线

使用 EventBus 实现全局事件通信 在 Vue 中,可以通过创建一个独立的 Vue 实例作为事件总线(EventBus)来实现组件间的通信。这种方法适用于非父子组件或跨层级组件之间的通信。…

vue 实现日历

vue 实现日历

实现基础日历结构 使用 Vue 的模板语法构建日历的网格布局,通常以 7 列(一周)和 5-6 行(月份天数)的表格形式展示。通过 v-for 循环渲染日期单元格,动态绑定样式和事件。 &l…

vue实现日历

vue实现日历

Vue 实现日历组件 使用 Vue 实现日历组件可以通过自定义开发或借助第三方库完成。以下是两种常见方法: 方法一:自定义日历组件 创建基础日历结构,利用 Vue 的响应式特性动态渲染日期。核心逻…

vue事件实现

vue事件实现

Vue 事件实现方法 模板中直接绑定事件 在 Vue 模板中通过 v-on 或 @ 语法直接绑定事件。例如点击事件: <button @click="handleClick">点击触发事…

jquery事件绑定

jquery事件绑定

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

vue实现事件监听

vue实现事件监听

Vue 事件监听实现方法 Vue 提供了多种方式实现事件监听,涵盖组件内外交互、原生 DOM 事件处理等场景。以下是核心实现方案: 模板内直接监听 通过 v-on 或 @ 语法监听 DOM 事件:…