当前位置:首页 > 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>

核心逻辑

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)

组件使用

<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实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并触发点击事件实现文件下载。适用于直接下载服务器上的静态文件或 Blob 数据。 downloadFile(url, fileName) {…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…