【问题标题】:Error fullcalendar lib before attempting to import a plugin angular在尝试导入插件 angular 之前出错 fullcalendar lib
【发布时间】:2020-12-27 20:38:12
【问题描述】:

我有这个错误:

Error: Please import the top-level fullcalendar lib before attempting to import a plugin.

当我将 resourceTimeGridPlugin 添加到插件时。

我的代码:

import dayGridPlugin from '@fullcalendar/daygrid';
import interactionPlugin, { Draggable } from '@fullcalendar/interaction';
import { FullCalendarComponent } from '@fullcalendar/angular';
import timeGridPlugin from '@fullcalendar/timegrid';
import resourceTimeGridPlugin from '@fullcalendar/resource-timegrid';

【问题讨论】:

    标签: javascript angular webpack types fullcalendar


    【解决方案1】:

    您必须在组件中包含日历核心对象:

    import { Calendar } from '@fullcalendar/core';
    

    在你的构造函数中你必须添加这个:

     constructor() { 
        const name = Calendar.name; 
     }
    

    完整示例:

    import { Component } from '@angular/core';
    import { Calendar } from '@fullcalendar/core';
    import dayGridPlugin from '@fullcalendar/daygrid';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
    
      calendarOptions = {
        plugins: [dayGridPlugin],
        initialView: 'dayGridMonth'
      };
    
     constructor() { 
        const name = Calendar.name;
      }
    }
    

    这会在插件需要之前强制加载/初始化核心组件和 VDom 引用。

    【讨论】:

    • 现在可以,但是随着代码的修改,如果 Calendar 没有被使用,TypeScript 将忽略导入,它会再次失败。为避免这种可能性,请明确说明,因为对此有语法。 import '@fullcalendar/core';。这意味着导入此模块以获取其副作用。
    【解决方案2】:

    这就是我在项目中为解决这个问题所做的。

    在我的模块中按此顺序导入并注册我的插件。

    import { FullCalendarModule } from "@fullcalendar/angular";
    import dayGridPlugin from '@fullcalendar/daygrid';
    import interactionPlugin from '@fullcalendar/interaction';
    import listPlugin from '@fullcalendar/list';
    import momentPlugin from '@fullcalendar/moment';
    import rrulePlugin from '@fullcalendar/rrule';
    import timeGridPlugin from '@fullcalendar/timegrid';
    
    FullCalendarModule.registerPlugins([
      dayGridPlugin,
      interactionPlugin,
      listPlugin,
      momentPlugin,
      rrulePlugin,
      timeGridPlugin
    ]);
    

    在我准备 calendarOptions 之前,我在构造函数中使用这个组件

    forwardRef(() =>Calendar);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-02
      • 2022-12-14
      • 2022-11-09
      • 2022-12-19
      • 1970-01-01
      • 2017-12-26
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多