当前位置:首页 > VUE

vue页面实现日历

2026-03-07 21:35:17VUE

实现Vue日历组件的基础方法

使用Vue实现日历功能可以通过原生开发或借助第三方库完成。以下是两种常见实现方式:

原生实现方案 创建Vue组件,计算当前月份的天数及排列方式:

<template>
  <div class="calendar">
    <div class="header">{{ currentMonth }}</div>
    <div class="days">
      <div v-for="day in dayNames" :key="day">{{ day }}</div>
    </div>
    <div class="dates">
      <div 
        v-for="date in visibleDates" 
        :key="date.getTime()"
        :class="{ 'other-month': !isCurrentMonth(date) }"
      >
        {{ date.getDate() }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      dayNames: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { 
        month: 'long', 
        year: 'numeric' 
      })
    },
    visibleDates() {
      const start = this.getMonthStart()
      const end = this.getMonthEnd()
      const dates = []

      for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
        dates.push(new Date(d))
      }

      return dates
    }
  },
  methods: {
    isCurrentMonth(date) {
      return date.getMonth() === this.currentDate.getMonth()
    },
    getMonthStart() {
      const date = new Date(this.currentDate)
      date.setDate(1)
      const day = date.getDay()
      date.setDate(date.getDate() - day)
      return date
    },
    getMonthEnd() {
      const date = new Date(this.currentDate)
      date.setMonth(date.getMonth() + 1)
      date.setDate(0)
      const day = date.getDay()
      date.setDate(date.getDate() + (6 - day))
      return date
    }
  }
}
</script>

使用第三方库的实现方案

对于更复杂的日历需求,可以考虑以下流行库:

vue页面实现日历

FullCalendar集成 安装依赖:

npm install @fullcalendar/vue @fullcalendar/daygrid

基础实现代码:

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',
        headerToolbar: {
          left: 'prev,next today',
          center: 'title',
          right: 'dayGridMonth,dayGridWeek'
        },
        events: [
          { title: '会议', start: '2023-05-01' },
          { title: '生日', start: '2023-05-10' }
        ]
      }
    }
  }
}
</script>

日历功能扩展实现

添加事件管理 在原生实现中增加事件处理:

methods: {
  handleDateClick(date) {
    this.selectedDate = date
    this.showEventModal = true
  },
  addEvent(event) {
    this.events.push({
      date: this.selectedDate,
      title: event.title,
      description: event.description
    })
  }
}

实现月视图切换 添加月份导航功能:

methods: {
  prevMonth() {
    this.currentDate.setMonth(this.currentDate.getMonth() - 1)
    this.currentDate = new Date(this.currentDate)
  },
  nextMonth() {
    this.currentDate.setMonth(this.currentDate.getMonth() + 1)
    this.currentDate = new Date(this.currentDate)
  }
}

样式优化建议

基础日历样式示例:

.calendar {
  width: 350px;
  font-family: Arial;
}

.header {
  text-align: center;
  padding: 10px;
  font-size: 1.2em;
}

.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  padding: 5px 0;
}

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

.dates > div {
  padding: 10px;
  text-align: center;
  border-radius: 50%;
  cursor: pointer;
}

.dates > div.other-month {
  opacity: 0.3;
}

.dates > div:hover {
  background-color: #eee;
}

以上方案可根据实际需求进行组合或调整,原生实现适合简单需求,第三方库方案适合需要丰富功能的场景。

标签: 日历页面
分享给朋友:

相关文章

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要分为编程式导航和声明式导航两种。以下是具体实现方法: 使用 <router-link> 声明式导航 <r…

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 Vue 中实现页面切换通常使用 Vue Router,这是 Vue.js 官方的路由管理器。以下是几种常见的实现方式: 使用 Vue Router 的基本配置 安装 Vu…

vue实现关闭页面

vue实现关闭页面

关闭当前页面的方法 在Vue中关闭当前页面通常涉及调用浏览器提供的JavaScript方法。以下是几种常见的实现方式: window.close()方法 methods: { closeWin…

react如何清除页面

react如何清除页面

清除页面内容的方法 在React中清除页面内容通常涉及重置组件状态或移除特定元素。以下是几种常见方法: 重置组件状态 通过将组件的状态重置为初始值来清除页面内容: const MyComponen…

react如何分割页面

react如何分割页面

在React中分割页面的方法 React提供了多种方式来实现页面的分割和模块化,以下是一些常见的方法: 使用组件化开发 将页面拆分为多个独立的组件,每个组件负责特定的功能或UI部分。通过组合这些组件…

js 实现页面刷新

js 实现页面刷新

使用 location.reload() location.reload() 是原生 JavaScript 提供的页面刷新方法。调用该方法会重新加载当前页面,相当于点击浏览器的刷新按钮。 // 基本…