当前位置:首页 > 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实现日历方案 使用第三方库(推荐方案) 推荐使用成熟的日历组件库,如v-calendar或fullcalendar-vue,它们提供丰富的功能和定制选项。 安装v-calendar: npm…

vue实现树形控件

vue实现树形控件

vue实现树形控件的方法 使用Vue实现树形控件可以通过递归组件或第三方库完成。以下是几种常见实现方式: 递归组件实现 递归组件是Vue实现树形结构的核心方法,通过组件自身调用自身实现无限层级嵌套。…

react 控件值如何刷新

react 控件值如何刷新

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

react简单实现树形控件

react简单实现树形控件

实现树形控件的基本思路 树形控件的核心是递归渲染节点,每个节点可以包含子节点。通过状态管理展开/折叠状态,动态渲染可见部分。 基础代码结构 创建一个可复用的树形组件,包含以下关键部分: impor…

react实现日历翻页效果

react实现日历翻页效果

实现日历翻页效果的核心思路 日历翻页效果通常涉及日期数据的动态切换和动画过渡。React中可通过状态管理日期数据,结合CSS动画或第三方动画库实现视觉翻页效果。 基础实现步骤 状态管理 使用Reac…

css日历制作

css日历制作

CSS日历制作方法 使用HTML和CSS创建一个简单的日历布局,可以通过以下方式实现: HTML结构 <div class="calendar"> <div class="m…