当前位置:首页 > VUE

vue实现日历控件

2026-03-28 15:03:31VUE

Vue 实现日历控件的方法

使用第三方库(如 FullCalendar)

安装 FullCalendar 及其 Vue 适配器:

npm install @fullcalendar/vue @fullcalendar/core @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',
        events: [
          { title: 'Meeting', start: new Date() }
        ]
      }
    }
  }
}
</script>

自定义日历组件

创建基础的日历模板结构:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">←</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">→</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.getTime()"
        :class="{ 'other-month': !isCurrentMonth(date) }"
        @click="selectDate(date)"
      >
        {{ date.getDate() }}
      </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 start = this.getMonthStart()
      const end = this.getMonthEnd()
      const dates = []

      let date = new Date(start)
      while (date <= end) {
        dates.push(new Date(date))
        date.setDate(date.getDate() + 1)
      }

      return dates
    }
  },
  methods: {
    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
    },
    isCurrentMonth(date) {
      return date.getMonth() === this.currentDate.getMonth()
    },
    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)
    },
    selectDate(date) {
      this.$emit('date-selected', date)
    }
  }
}
</script>

添加基础样式:

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

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 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;
}

.dates div {
  padding: 10px;
  text-align: center;
  cursor: pointer;
}

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

.other-month {
  color: #ccc;
}

功能扩展建议

添加事件管理功能,允许用户为特定日期添加事件:

data() {
  return {
    events: {}
  }
},
methods: {
  addEvent(date, event) {
    const key = date.toDateString()
    this.events[key] = this.events[key] || []
    this.events[key].push(event)
    this.$forceUpdate()
  }
}

在模板中显示事件:

<template>
  <div class="date-cell" @click="selectDate(date)">
    {{ date.getDate() }}
    <div v-for="(event, index) in events[date.toDateString()]" :key="index">
      {{ event }}
    </div>
  </div>
</template>

实现日期范围选择功能:

vue实现日历控件

data() {
  return {
    selectedStart: null,
    selectedEnd: null
  }
},
methods: {
  selectDate(date) {
    if (!this.selectedStart || this.selectedEnd) {
      this.selectedStart = date
      this.selectedEnd = null
    } else {
      this.selectedEnd = date
      if (this.selectedEnd < this.selectedStart) {
        [this.selectedStart, this.selectedEnd] = [this.selectedEnd, this.selectedStart]
      }
      this.$emit('range-selected', {
        start: this.selectedStart,
        end: this.selectedEnd
      })
    }
  }
}

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

相关文章

vue实现日历人员值班

vue实现日历人员值班

实现日历人员值班功能 日历人员值班功能可以通过Vue结合第三方日历组件或自定义实现。以下是一种基于FullCalendar的实现方案。 安装依赖 需要安装@fullcalendar/vue及相关插件…

vue实现钉钉日历

vue实现钉钉日历

使用 Vue 实现钉钉日历功能 安装依赖 需要安装 v-calendar 或 fullcalendar-vue 等日历库。以 v-calendar 为例: npm install v-calenda…

vue实现iview日历显示

vue实现iview日历显示

实现步骤 安装必要的依赖库iview和vue。通过npm或yarn进行安装: npm install iview vue 在main.js中引入iview并注册组件: import Vue fro…

vue实现日历周滑动

vue实现日历周滑动

Vue 实现日历周滑动功能 核心思路 日历周滑动功能通常需要结合日期计算、动态渲染和触摸事件处理。Vue 的响应式特性可以轻松实现数据与视图的同步更新。 日期计算逻辑 使用 Date 对象或第三方库…

react 控件值如何刷新

react 控件值如何刷新

手动触发重新渲染 在React中,组件的状态(state)或属性(props)发生变化时,会自动触发重新渲染。可以通过setState或useState的更新函数来修改状态值。 const [cou…

react中如何获得某一控件

react中如何获得某一控件

获取控件的方法 在React中获取DOM元素或组件实例可以通过多种方式实现,具体取决于使用场景和组件类型(类组件或函数组件)。 使用ref属性 通过ref属性可以直接访问DOM节点或类组件实例。在函…