当前位置:首页 > VUE

vue 日历组件实现

2026-01-19 01:50:18VUE

Vue 日历组件实现

基本结构搭建

使用Vue的单文件组件(SFC)结构,创建一个基础的日历组件框架。需要定义月份、年份的状态管理,以及日期渲染逻辑。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h2>{{ currentYear }}年{{ currentMonth }}月</h2>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="days-grid">
      <div v-for="day in days" :key="day" class="day-header">{{ day }}</div>
      <div 
        v-for="date in visibleDates" 
        :key="date.getTime()"
        class="date-cell"
        :class="{ 'other-month': !isCurrentMonth(date) }"
      >
        {{ date.getDate() }}
      </div>
    </div>
  </div>
</template>

核心逻辑实现

需要计算当前月份的所有日期,包括上个月和下个月的部分日期以确保日历网格完整。

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      days: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear()
    },
    currentMonth() {
      return this.currentDate.getMonth() + 1
    },
    visibleDates() {
      const dates = []
      const firstDay = new Date(this.currentYear, this.currentDate.getMonth(), 1)
      const lastDay = new Date(this.currentYear, this.currentDate.getMonth() + 1, 0)

      // 上个月的最后几天
      const prevMonthDays = firstDay.getDay()
      for (let i = prevMonthDays - 1; i >= 0; i--) {
        const date = new Date(firstDay)
        date.setDate(date.getDate() - (i + 1))
        dates.push(date)
      }

      // 当前月的所有日期
      for (let i = 1; i <= lastDay.getDate(); i++) {
        dates.push(new Date(this.currentYear, this.currentDate.getMonth(), i))
      }

      // 下个月的前几天
      const nextMonthDays = 6 - lastDay.getDay()
      for (let i = 1; i <= nextMonthDays; i++) {
        const date = new Date(lastDay)
        date.setDate(date.getDate() + i)
        dates.push(date)
      }

      return dates
    }
  },
  methods: {
    isCurrentMonth(date) {
      return date.getMonth() === this.currentDate.getMonth()
    },
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentDate.getMonth() - 1, 1)
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentDate.getMonth() + 1, 1)
    }
  }
}
</script>

样式设计

为日历添加基本样式,确保布局合理美观。

<style scoped>
.calendar {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.days-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.day-header {
  text-align: center;
  font-weight: bold;
  padding: 5px;
}

.date-cell {
  text-align: center;
  padding: 10px;
  border: 1px solid #eee;
  cursor: pointer;
}

.date-cell:hover {
  background-color: #f5f5f5;
}

.other-month {
  color: #ccc;
}
</style>

功能扩展

可以添加日期选择功能,标记特定日期等高级功能。

// 在methods中添加
selectDate(date) {
  this.$emit('date-selected', date)
}

// 在模板中修改date-cell
<div 
  v-for="date in visibleDates" 
  :key="date.getTime()"
  class="date-cell"
  :class="{ 
    'other-month': !isCurrentMonth(date),
    'selected': isSelected(date)
  }"
  @click="selectDate(date)"
>
  {{ date.getDate() }}
</div>

// 添加isSelected方法
isSelected(date) {
  return this.selectedDate && 
         date.getDate() === this.selectedDate.getDate() &&
         date.getMonth() === this.selectedDate.getMonth() &&
         date.getFullYear() === this.selectedDate.getFullYear()
}

第三方库集成

对于更复杂的需求,可以考虑使用成熟的日历库如v-calendar或fullcalendar-vue。

vue 日历组件实现

npm install v-calendar
import VCalendar from 'v-calendar'

// 在main.js中
Vue.use(VCalendar)

// 组件中使用
<template>
  <v-calendar />
</template>

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

相关文章

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

组件elementui

组件elementui

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如按钮、表单、表格等),适合快速构建企业级中后台产品。…

vue实现search组件

vue实现search组件

Vue 实现搜索组件 基础实现 创建一个基础的搜索组件需要包含输入框和搜索逻辑。以下是一个简单的实现示例: <template> <div class="search-cont…

vue 动态组件实现

vue 动态组件实现

vue 动态组件实现 Vue 的动态组件功能允许根据条件或用户交互动态切换不同的组件,主要通过 <component> 标签和 is 属性实现。 基本用法 通过 is 属性绑定组件名或组…

vue实现组件缓存

vue实现组件缓存

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