当前位置:首页 > VUE

vue日历组件实现

2026-02-11 03:09:06VUE

Vue 日历组件实现方法

基础日历结构

使用 Vue 的模板语法构建日历骨架,通过 v-for 循环渲染日期格子。核心是计算当前月份的天数和起始星期位置。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上个月</button>
      <span>{{ currentYear }}年{{ currentMonth + 1 }}月</span>
      <button @click="nextMonth">下个月</button>
    </div>
    <div class="weekdays">
      <div v-for="day in ['日','一','二','三','四','五','六']" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(day, index) in days" 
        :key="index"
        :class="{ 'other-month': !day.isCurrentMonth }"
        @click="selectDay(day)"
      >
        {{ day.date.getDate() }}
      </div>
    </div>
  </div>
</template>

日期计算逻辑

在 Vue 的 datasetup 中定义响应式变量,通过计算方法生成当月日历数据:

export default {
  data() {
    return {
      currentDate: new Date(),
      days: []
    }
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear()
    },
    currentMonth() {
      return this.currentDate.getMonth()
    }
  },
  methods: {
    generateDays() {
      const year = this.currentYear
      const month = this.currentMonth
      const firstDay = new Date(year, month, 1)
      const lastDay = new Date(year, month + 1, 0)

      const startDay = firstDay.getDay()
      const totalDays = lastDay.getDate()

      const daysArray = []

      // 上个月末尾几天
      const prevMonthLastDay = new Date(year, month, 0).getDate()
      for (let i = 0; i < startDay; i++) {
        daysArray.push({
          date: new Date(year, month - 1, prevMonthLastDay - i),
          isCurrentMonth: false
        })
      }
      daysArray.reverse()

      // 当月日期
      for (let i = 1; i <= totalDays; i++) {
        daysArray.push({
          date: new Date(year, month, i),
          isCurrentMonth: true
        })
      }

      // 下个月开头几天
      const remaining = 42 - daysArray.length
      for (let i = 1; i <= remaining; i++) {
        daysArray.push({
          date: new Date(year, month + 1, i),
          isCurrentMonth: false
        })
      }

      this.days = daysArray
    },
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth - 1, 1)
      this.generateDays()
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth + 1, 1)
      this.generateDays()
    },
    selectDay(day) {
      console.log('Selected:', day.date)
    }
  },
  mounted() {
    this.generateDays()
  }
}

样式增强

添加基础样式使日历更美观:

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

高级功能扩展

  1. 日期范围选择:添加 startDateendDate 状态,通过比较逻辑标记选中范围
  2. 事件标记:接收外部事件数据,在日期格子上显示标记点
  3. 国际化:支持多语言星期显示和日期格式
  4. 自定义插槽:允许用户自定义日期格子的渲染内容
// 范围选择示例
methods: {
  isInRange(date) {
    if (!this.startDate || !this.endDate) return false
    return date >= this.startDate && date <= this.endDate
  },
  handleDayClick(day) {
    if (!this.startDate || (this.startDate && this.endDate)) {
      this.startDate = day.date
      this.endDate = null
    } else {
      this.endDate = day.date
      if (this.endDate < this.startDate) {
        [this.startDate, this.endDate] = [this.endDate, this.startDate]
      }
    }
  }
}

第三方库方案

对于生产环境,可以考虑以下成熟库:

  • Vue DatePicker:轻量级且高度可配置
  • FullCalendar:功能全面的日历组件,支持拖拽等高级功能
  • V-Calendar:提供丰富的日期选择器和日历视图

安装示例:

npm install v-calendar

基础使用:

import VCalendar from 'v-calendar'

// 在组件中
<v-calendar :attributes="attributes" />

以上方案可根据实际需求组合使用,基础实现约100行代码即可完成核心功能,扩展功能需根据具体场景增加相应逻辑。

vue日历组件实现

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

相关文章

vue组件的实现

vue组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,主要包括单文件组件(SFC)、全局注册和局部注册。以下是常见的实现方法: 单文件组件(SFC) 单文件组件是 Vue 最推荐的组件化开发方…

实现vue组件

实现vue组件

创建 Vue 组件 Vue 组件可以通过单文件组件(.vue 文件)或直接在 JavaScript 中定义。以下是两种常见实现方式。 单文件组件方式 单文件组件包含模板、脚本和样式三部分,适合复杂项…

vue实现日历

vue实现日历

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

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class…

vue怎么实现日历

vue怎么实现日历

实现基础日历布局 使用Vue的模板语法构建日历的HTML结构,通常采用表格形式展示日期。月份切换通过计算属性动态生成日期数组。 <template> <div class=…

react组件如何通讯

react组件如何通讯

React 组件通讯方式 React 组件间的通讯方式主要包括以下几种方法,适用于不同场景下的数据传递和状态管理需求。 父子组件通讯(Props 传递) 父组件通过 props 向子组件传递数据或…