一、 样图
1. 月视图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210318165443166.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDMzMjE3,size_16,color_FFFFFF,t_70)
2. 周视图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210318165514313.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDMzMjE3,size_16,color_FFFFFF,t_70)
3. 日视图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210318170547832.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDMzMjE3,size_16,color_FFFFFF,t_70)
二、 依赖
1. 在package.json中引入依赖
"dependencies": {
"@fullcalendar/core": "^4.3.1",
"@fullcalendar/moment": "^5.5.0",
"@fullcalendar/daygrid": "^5.5.0",
"@fullcalendar/interaction": "^5.5.0",
"@fullcalendar/timegrid": "^5.5.0",
"@fullcalendar/vue": "^5.5.0"
}
2. 执行以下命令下载依赖
npm install --save @fullcalendar/vue @fullcalendar/daygrid @fullcalendar/timegrid @fullcalendar/moment @fullcalendar/interaction
三、代码
1. demo代码
<template>
<div class='demo-app'>
<div class='demo-app-main'>
<FullCalendar class='demo-app-calendar' :options='calendarOptions'>
<template v-slot:eventContent='arg'>
<b>{{ arg.timeText }}</b>
<i>{{ arg.event.title }}</i>
</template>
</FullCalendar>
</div>
</div>
</template>
<script>
import FullCalendar from '@fullcalendar/vue'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import interactionPlugin from '@fullcalendar/interaction'
import { INITIAL_EVENTS, createEventId } from '../../../event-utils'
export default {
components: {
FullCalendar,
},
data: function () {
return {
calendarOptions: {
plugins: [
dayGridPlugin,
timeGridPlugin,
interactionPlugin,
],
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay',
},
initialView: 'dayGridMonth',
initialEvents: INITIAL_EVENTS,
editable: true,
selectable: true,
selectMirror: true,
dayMaxEvents: true,
weekends: true,
select: this.handleDateSelect,
eventClick: this.handleEventClick,
eventsSet: this.handleEvents,
height: 700,
locale: 'zh-cn',
allDaySlot: false,
scrollTime: '09:00:00',
firstDay: 1,
minTime: '06:00:00',
maxTime: '21:00:00',
slotLabelFormat: {
hour: 'numeric',
minute: '2-digit',
hour12: false,
},
businessHours: {
dow: [1, 2, 3, 4,5],
start: '10:00',
end: '18:00',
},
slotEventOverlap: false,
agendaEventMinHeight: true,
},
currentEvents: [],
}
},
methods: {
handleWeekendsToggle() {
this.calendarOptions.weekends = !this.calendarOptions.weekends
},
handleDateSelect(selectInfo) {
let title = prompt('Please enter a new title for your event')
let calendarApi = selectInfo.view.calendar
calendarApi.unselect()
if (title) {
calendarApi.addEvent({
id: createEventId(),
title,
start: selectInfo.startStr,
end: selectInfo.endStr,
allDay: selectInfo.allDay,
})
}
},
handleEventClick(clickInfo) {
if (
confirm(
`Are you sure you want to delete the event '${clickInfo.event.title}'`
)
) {
clickInfo.event.remove()
}
},
handleEvents(events) {
this.currentEvents = events
},
},
}
</script>
<style lang='css'>
b {
margin-right: 3px;
}
.demo-app {
display: flex;
min-height: 100%;
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
font-size: 14px;
}
.demo-app-sidebar {
width: 300px;
line-height: 1.5;
background: #eaf9ff;
border-right: 1px solid #d3e2e8;
}
.demo-app-sidebar-section {
padding: 2em;
}
.demo-app-main {
flex-grow: 1;
padding: 3em;
}
.fc {
max-width: 1100px;
margin: 0 auto;
}
</style>
2. event-utils.js
let eventGuid = 0
let todayStr = new Date().toISOString().replace(/T.*$/, '') // YYYY-MM-DD of today
export const INITIAL_EVENTS = [
{
id: createEventId(),
title: 'All-day event',
start: todayStr
},
{
id: createEventId(),
title: 'Timed event',
start: todayStr + 'T12:00:00'
}
]
export function createEventId() {
return String(eventGuid++)
}