当前位置:首页 > VUE

vue实现日历

2026-01-07 23:11:52VUE

Vue 实现日历的基本方法

使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式:

自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当月日期数组,并处理跨月日期。

使用第三方库v-calendarfullcalendar-vue 等成熟库可快速实现功能丰富的日历。

自定义日历组件实现步骤

安装依赖(如需日期处理)

npm install date-fns

组件基本结构

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="weekdays">
      <div v-for="day in weekdays" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(date, index) in days" 
        :key="index"
        :class="{ 'other-month': !date.isCurrentMonth }"
        @click="selectDate(date)"
      >
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

核心逻辑

vue实现日历

import { format, addMonths, getDaysInMonth, startOfMonth, getDay } from 'date-fns'

export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentMonth() {
      return format(this.currentDate, 'yyyy年MM月')
    },
    days() {
      const daysInMonth = getDaysInMonth(this.currentDate)
      const startDay = getDay(startOfMonth(this.currentDate))
      const days = []

      // 上月剩余天数
      const prevMonthDays = startDay === 0 ? 6 : startDay - 1
      const prevMonth = addMonths(this.currentDate, -1)
      const prevMonthLastDay = getDaysInMonth(prevMonth)

      for (let i = prevMonthLastDay - prevMonthDays + 1; i <= prevMonthLastDay; i++) {
        days.push({
          day: i,
          date: new Date(prevMonth.getFullYear(), prevMonth.getMonth(), i),
          isCurrentMonth: false
        })
      }

      // 当月天数
      for (let i = 1; i <= daysInMonth; i++) {
        days.push({
          day: i,
          date: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), i),
          isCurrentMonth: true
        })
      }

      // 下月天数补全
      const remainingDays = 42 - days.length
      const nextMonth = addMonths(this.currentDate, 1)

      for (let i = 1; i <= remainingDays; i++) {
        days.push({
          day: i,
          date: new Date(nextMonth.getFullYear(), nextMonth.getMonth(), i),
          isCurrentMonth: false
        })
      }

      return days
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = addMonths(this.currentDate, -1)
    },
    nextMonth() {
      this.currentDate = addMonths(this.currentDate, 1)
    },
    selectDate(date) {
      this.$emit('date-selected', date)
    }
  }
}

基础样式

.calendar {
  width: 350px;
  font-family: Arial;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-weight: bold;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 5px;
}
.days > div {
  padding: 10px;
  text-align: center;
  cursor: pointer;
}
.days > div:hover {
  background: #eee;
}
.other-month {
  color: #ccc;
}

使用 v-calendar 库实现

安装 v-calendar

npm install v-calendar

全局注册

import VCalendar from 'v-calendar'
import 'v-calendar/dist/style.css'

Vue.use(VCalendar)

组件使用

vue实现日历

<template>
  <v-calendar
    :attributes="attributes"
    @dayclick="onDayClick"
  />
</template>

<script>
export default {
  data() {
    return {
      attributes: [
        {
          highlight: 'blue',
          dates: new Date()
        }
      ]
    }
  },
  methods: {
    onDayClick(day) {
      console.log('Selected date:', day.date)
    }
  }
}
</script>

日历功能扩展建议

事件标记 在日期上显示事件或标记,可通过在日期数据中添加事件属性实现。

范围选择 实现日期范围选择功能,需要处理开始日期和结束日期的逻辑。

国际化 支持多语言周显示和日期格式,可使用 date-fns 的 locale 功能。

主题定制 通过 CSS 变量或预处理器定制日历样式,适应不同设计需求。

移动端适配 添加触摸事件支持,优化小屏幕显示效果。

标签: 日历vue
分享给朋友:

相关文章

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…