当前位置:首页 > VUE

vue如何实现日历

2026-02-20 11:38:55VUE

Vue 日历实现方法

使用第三方库(如 FullCalendar)

FullCalendar 是一个功能强大的日历库,支持 Vue 集成。安装依赖后,可以直接在组件中使用。

npm install @fullcalendar/vue @fullcalendar/core @fullcalendar/daygrid

在 Vue 组件中引入并配置:

vue如何实现日历

<template>
  <FullCalendar :options="calendarOptions" />
</template>

<script>
import FullCalendar from '@fullcalendar/vue'
import dayGridPlugin from '@fullcalendar/daygrid'

export default {
  components: { FullCalendar },
  data() {
    return {
      calendarOptions: {
        plugins: [dayGridPlugin],
        initialView: 'dayGridMonth',
        events: [
          { title: 'Meeting', start: '2023-10-01' }
        ]
      }
    }
  }
}
</script>

自定义日历组件

手动实现一个基础日历,需要处理日期计算和渲染逻辑。

vue如何实现日历

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">Previous</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">Next</button>
    </div>
    <div class="days">
      <div v-for="day in days" :key="day" class="day-header">{{ day }}</div>
    </div>
    <div class="dates">
      <div v-for="date in visibleDates" :key="date.date" class="date" :class="{ 'other-month': !date.isCurrentMonth }">
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { month: 'long', year: 'numeric' })
    },
    visibleDates() {
      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 prevDays = firstDay.getDay()
      const nextDays = 6 - lastDay.getDay()
      const dates = []

      for (let i = prevDays; i > 0; i--) {
        const date = new Date(year, month, -i + 1)
        dates.push({ day: date.getDate(), isCurrentMonth: false })
      }

      for (let i = 1; i <= lastDay.getDate(); i++) {
        dates.push({ day: i, isCurrentMonth: true })
      }

      for (let i = 1; i <= nextDays; i++) {
        const date = new Date(year, month + 1, i)
        dates.push({ day: date.getDate(), isCurrentMonth: false })
      }

      return dates
    }
  },
  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)
    }
  }
}
</script>

<style>
.calendar {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
}
.dates {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 5px;
}
.date {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
}
.other-month {
  opacity: 0.5;
}
</style>

使用 Vuetify 的 v-calendar

如果项目使用 Vuetify,可以直接使用其内置的日历组件。

<template>
  <v-calendar
    :events="events"
    :now="today"
    :value="today"
    color="primary"
  ></v-calendar>
</template>

<script>
export default {
  data() {
    return {
      today: '2023-10-01',
      events: [
        {
          name: 'Meeting',
          start: '2023-10-01',
          end: '2023-10-01',
          color: 'blue'
        }
      ]
    }
  }
}
</script>

功能扩展

日历组件可以根据需求添加更多功能:

  • 事件点击处理
  • 拖拽调整事件
  • 日期范围选择
  • 多视图切换(日/周/月)

以上方法提供了从简单到复杂的 Vue 日历实现方案,可根据项目需求选择合适的实现方式。

分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现思路 在Vue中实现到期提醒功能,可以通过计算日期差、定时检查和通知用户三个核心步骤完成。需要结合Vue的响应式特性和JavaScript的日期处理能力。 计算日期差 使用JavaScr…

vue如何实现记住我

vue如何实现记住我

实现“记住我”功能的步骤 在Vue中实现“记住我”功能通常涉及前端保存用户登录状态(如token)到本地存储,并在下次访问时自动恢复登录状态。以下是具体实现方法: 使用localStorage或…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Paren…

php如何实现静态化

php如何实现静态化

PHP 实现静态化的方法 使用 ob_start() 和 ob_get_contents() 利用 PHP 的输出缓冲功能捕获动态生成的页面内容,将其保存为静态文件。这种方法适用于内容不频繁变化的页面…

css制作日历

css制作日历

使用CSS Grid布局制作日历 日历的布局适合使用CSS Grid实现,因其天然的行列结构。以下是一个基础日历的HTML和CSS代码: <div class="calendar">…

vue如何实现轮询

vue如何实现轮询

实现轮询的方法 在Vue中实现轮询可以通过以下几种方式完成,具体选择取决于项目需求和场景。 使用 setInterval 通过 setInterval 定时调用接口或执行任务,适合简单的轮询场景。…