当前位置:首页 > 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 实现的基本方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是一些常见的实现方法和步骤: 安装 Vue.js 可以通过 CDN 引入 Vue.js,或者使用…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…