当前位置:首页 > VUE

vue VUE实现日历组件功能

2026-01-14 00:46:16VUE

Vue 日历组件实现方法

基础日历结构

使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上个月</button>
      <span>{{ currentYear }}年{{ currentMonth }}月</span>
      <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="(day, index) in days" 
        :key="index"
        :class="{ 'other-month': !day.isCurrentMonth }"
        @click="selectDay(day)"
      >
        {{ day.date }}
      </div>
    </div>
  </div>
</template>

日期数据处理

计算当前月份的天数和前后月份的补全日期,确保日历表格完整显示6行。

vue VUE实现日历组件功能

data() {
  return {
    currentDate: new Date(),
    weekdays: ['日', '一', '二', '三', '四', '五', '六']
  }
},
computed: {
  days() {
    const year = this.currentDate.getFullYear()
    const month = this.currentDate.getMonth()
    const firstDay = new Date(year, month, 1)
    const lastDay = new Date(year, month + 1, 0)
    const days = []

    // 上个月补全天数
    const prevMonthDays = firstDay.getDay()
    for (let i = prevMonthDays - 1; i >= 0; i--) {
      days.push({
        date: new Date(year, month, -i).getDate(),
        isCurrentMonth: false
      })
    }

    // 当月天数
    for (let i = 1; i <= lastDay.getDate(); i++) {
      days.push({
        date: i,
        isCurrentMonth: true,
        fullDate: new Date(year, month, i)
      })
    }

    // 下个月补全天数
    const nextMonthDays = 42 - days.length
    for (let i = 1; i <= nextMonthDays; i++) {
      days.push({
        date: i,
        isCurrentMonth: false
      })
    }

    return days
  },
  currentYear() {
    return this.currentDate.getFullYear()
  },
  currentMonth() {
    return this.currentDate.getMonth() + 1
  }
}

月份切换功能

实现上个月和下个月的切换逻辑,更新currentDate数据。

vue VUE实现日历组件功能

methods: {
  prevMonth() {
    this.currentDate = new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth() - 1,
      1
    )
  },
  nextMonth() {
    this.currentDate = new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth() + 1,
      1
    )
  },
  selectDay(day) {
    if (day.isCurrentMonth) {
      console.log('选择的日期:', day.fullDate)
    }
  }
}

样式设计

添加基础CSS样式,使日历显示美观。

.calendar {
  width: 350px;
  font-family: Arial;
}
.header {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background: #f0f0f0;
}
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: #e0e0e0;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 1px;
  background: #ccc;
}
.days > div {
  padding: 10px;
  background: white;
  height: 40px;
}
.other-month {
  color: #aaa;
}

进阶功能扩展

可根据需求添加以下功能:

  • 日期范围选择
  • 标记特殊日期
  • 多语言支持
  • 事件显示
  • 移动端适配
// 示例:标记今天
computed: {
  days() {
    // ...原有代码
    const today = new Date()
    days.forEach(day => {
      if (day.fullDate && 
          day.fullDate.getDate() === today.getDate() &&
          day.fullDate.getMonth() === today.getMonth() &&
          day.fullDate.getFullYear() === today.getFullYear()) {
        day.isToday = true
      }
    })
    return days
  }
}
/* 添加今天样式 */
.today {
  background: #ffeb3b !important;
  font-weight: bold;
}

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

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

如何缓存react组件

如何缓存react组件

缓存React组件的方法 React.memo 使用React.memo对函数组件进行浅比较缓存,避免不必要的重新渲染。适用于props不变的场景。 const MemoizedComponen…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue实现高阶组件

vue实现高阶组件

Vue 高阶组件实现方法 高阶组件(HOC)是一种复用组件逻辑的模式,通过函数包裹组件并返回新组件实现。Vue 中可通过以下方式实现: 使用 render 函数 通过函数接收组件选项并返回新组件选…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-ali…