我如何获得 fullcalendar v.4.2.0 实例?

How can i get the fullcalendar v.4.2.0 instance?

所以我的页面中有一个 fullCalendar (v.4.2.0) 实例(loaded/built on document.ready),我想在其他函数中访问该 fullCalendar 实例以动态添加事件。

使用$('#calendar').fullCalendar('getView')显示错误,fullCalendar()方法不存在:

$(...).fullCalendar is not a function

如何访问该实例?

我用来生成日历的代码如下:

var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: [ 'timeGrid', 'dayGrid' ],
        defaultView: 'timeGridWeek', 
        height: 700,
        locale: 'pt',
        allDaySlot: false,
        handleWindowResize: true,
        events: [
            {
                title: 'Teste1', // a property!
                start: '2019-07-23 16:00', 
                end: '2019-07-23 17:00', 
                backgroundColor: '#fcba03',
                borderColor: '#fcba03',
            }
        ]
    });
calendar.render();
**Create a config variable like below in js**

uiConfig = {
        calendar: {
          height: 450,
          editable: true,
          eventClick: alertEventOnClick,
          eventDrop: alertOnDrop,
          eventResize: alertOnResize,
          eventRender: eventRender
        }
      };

**defines the above methods in js file**

alertEventOnClick = function (date, jsEvent, view) {}
alertOnDrop = function (date, jsEvent, view) {}
alertOnResize = function (date, jsEvent, view) {}
eventRender = function (date, jsEvent, view) {}

**and in html add below lines of code**

<div ui-calendar="uiConfig.calendar" id="myCalendar" calendar-watch-event="extraEventSignature(event)"
    calendar="myCalendar" data-ng-model="eventSources">
  </div>

**The Following code is another way of accessing calendar**

$('#myCalendar').fullCalendar({
  eventClick: function(event, element) {

    event.title = "CLICKED!";

    $('#calendar').fullCalendar('updateEvent', event);

  }
});

**where Mycalender should be added in HTML like below**

<div ui-calendar="uiConfig.calendar" id="myCalendar"
    calendar="myCalendar" data-ng-model="eventSources">
  </div>

由于您使用的是 fullCalendar 版本 4,它不再作为 jQuery 插件编写,因此无法使用 jQuery 语法来访问这些方法。相反,只需引用您的 calendar 变量(它可能需要在您的应用程序中具有广泛的范围 - 全局或通过其他方式轻松访问)。您会注意到,您的现有代码在调用 calendar.render();.

时已经完全做到了这一点

此外,在 "getView" 方法的特定情况下,这已被简单地替换为 view 属性,它引用当前视图。

例如(见最后一行):

var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: [ 'timeGrid', 'dayGrid' ],
        defaultView: 'timeGridWeek', 
        height: 700,
        locale: 'pt',
        allDaySlot: false,
        handleWindowResize: true,
        events: [
            {
                title: 'Teste1', // a property!
                start: '2019-07-23 16:00', 
                end: '2019-07-23 17:00', 
                backgroundColor: '#fcba03',
                borderColor: '#fcba03',
            }
        ]
    });

calendar.render();

var vw = calendar.view; //get the current view

参考文档

Documentation of the view property

Version 3 to Version 4 upgrade guide