当前位置:首页 > VUE

vue实现值班日历

2026-01-20 20:50:05VUE

Vue 实现值班日历的方法

使用 FullCalendar 插件

FullCalendar 是一个功能强大的日历插件,支持 Vue 集成。安装依赖:

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: '张三值班', start: '2023-10-01' },
          { title: '李四值班', start: '2023-10-02' }
        ]
      }
    }
  }
}
</script>

自定义实现日历组件

创建一个基础的日历组件,通过计算日期数组渲染日历格子:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上个月</button>
      <h3>{{ currentYear }}年{{ currentMonth + 1 }}月</h3>
      <button @click="nextMonth">下个月</button>
    </div>
    <div class="days">
      <div v-for="day in dayNames" :key="day" class="day-name">{{ day }}</div>
    </div>
    <div class="dates">
      <div 
        v-for="date in dates" 
        :key="date.date"
        :class="['date', { 'other-month': !date.isCurrentMonth }]"
      >
        {{ date.date.getDate() }}
        <div v-if="getDuty(date.date)" class="duty">
          {{ getDuty(date.date) }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentYear: new Date().getFullYear(),
      currentMonth: new Date().getMonth(),
      dayNames: ['日', '一', '二', '三', '四', '五', '六'],
      dutyData: {
        '2023-10-01': '张三',
        '2023-10-02': '李四'
      }
    }
  },
  computed: {
    dates() {
      // 计算当前月所有日期(包括前后月的补位)
    }
  },
  methods: {
    getDuty(date) {
      const key = date.toISOString().split('T')[0]
      return this.dutyData[key]
    }
  }
}
</script>

使用 Vuetify 的 v-calendar

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

<template>
  <v-calendar
    :events="events"
    type="month"
  ></v-calendar>
</template>

<script>
export default {
  data() {
    return {
      events: [
        {
          name: '张三值班',
          start: '2023-10-01',
          color: 'blue'
        }
      ]
    }
  }
}
</script>

关键实现点

  1. 日期计算:生成当前月所有日期数组,处理跨月显示
  2. 值班数据绑定:将值班人员数据与特定日期关联
  3. 交互功能:支持月份切换、值班信息编辑等操作
  4. 样式定制:根据需求调整日历的外观样式

可以根据项目需求选择合适的方法,FullCalendar 功能最全面但体积较大,自定义实现更轻量但需要处理更多细节。

标签: 日历vue
分享给朋友:

相关文章

vue日历表格实现

vue日历表格实现

Vue 日历表格实现方法 基础日历表格结构 使用 Vue 的模板语法构建日历表格框架,通过 v-for 循环生成日期单元格。核心是计算当前月份的天数及起始星期位置。 <template>…

vue实现录音

vue实现录音

Vue 实现录音功能 在 Vue 中实现录音功能可以通过浏览器提供的 MediaRecorder API 完成。以下是具体实现方法: 引入依赖 确保项目支持现代浏览器 API,无需额外安装库…

vue列表查询实现

vue列表查询实现

实现 Vue 列表查询功能 基础列表渲染 在 Vue 中可以通过 v-for 指令实现列表渲染,结合计算属性动态过滤数据: <template> <div> &l…

vue实现滑动条

vue实现滑动条

Vue 实现滑动条 在 Vue 中实现滑动条可以通过原生 HTML 的 <input type="range"> 或自定义组件实现。以下是两种常见方法: 使用原生 HTML 滑动条 通过…

vue页面实现日历

vue页面实现日历

Vue 页面实现日历的方法 使用第三方组件库 推荐使用成熟的日历组件库,如 v-calendar 或 fullcalendar-vue,快速实现功能丰富的日历。 安装 v-calendar: np…

vue实现border样式

vue实现border样式

实现边框样式的方法 在Vue中实现边框样式可以通过多种方式完成,包括内联样式、CSS类绑定、动态样式绑定等。以下是几种常见的方法: 内联样式绑定 通过style属性直接绑定样式对象,适用于简单的边…