当前位置:首页 > VUE

vue实现值班日历

2026-01-20 20:50:05VUE

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: '张三值班', 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-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…