How to use FullCalendar with Laravel Mix
Asked Answered
F

2

6

I have installed the Fullcalendar library packages using npm.

npm i --save @fullcalendar/core 
    @fullcalendar/interaction @fullcalendar/daygrid 
    @fullcalendar/timegrid @fullcalendar/list @fullcalendar/bootstrap

I've also included it in the app.js.

require('@fullcalendar/core');
require('@fullcalendar/bootstrap');
require('@fullcalendar/list');
require('@fullcalendar/timegrid');
require('@fullcalendar/daygrid');
require('@fullcalendar/interaction');

However when trying to create a calendar with the following code:

var calendarEl = document.getElementById('calendar');
calendar = new Calendar(calendarEl, {
    plugins: [ 'interactionPlugin', 'bootstrapPlugin', 'dayGridPlugin', 'timeGridPlugin', 'listPlugin'],
});

I get the error...

Uncaught ReferenceError: Calendar is not defined at HTMLDocument.<anonymous> on new Calendar

.

What am I doing wrong?

My webpack.mix.js looks like this:

const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css').version();

Edit:

After setting my app.js to:

window.FullCalendar = require('@fullcalendar/core');
window.bootstrapPlugin = require('@fullcalendar/bootstrap');
window.interactionPlugin = require('@fullcalendar/interaction');
window.listPlugin = require('@fullcalendar/list');
window.timeGridPlugin = require('@fullcalendar/timegrid');
window.dayGridPlugin = require('@fullcalendar/daygrid');

I now get this error: Uncaught TypeError: Cannot read property 'length' of undefined on

calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [bootstrapPlugin, interactionPlugin, listPlugin, timeGridPlugin, dayGridPlugin],
locale : 'pt-br',
})
Faller answered 14/10, 2020 at 19:16 Comment(0)
A
1

Some time has passed but I faced the same problem today and I've resolved.

  1. Install dependencies:
    npm install @fullcalendar/core @fullcalendar/daygrid @fullcalendar/timegrid @fullcalendar/list
  1. Import in javascript: NOTE: I've used a separate file called calendar.js and put it in resources/js/calendar.js, so i've added this to app.js
require('./calendar');
  1. Edit file calendar.js
    import { Calendar } from '@fullcalendar/core';
    import dayGridPlugin from '@fullcalendar/daygrid';
    import timeGridPlugin from '@fullcalendar/timegrid';
    import listPlugin from '@fullcalendar/list';
    import itLocale from '@fullcalendar/core/locales/it'; //italian language and config
  1. Edit the file adding (after the import section)
document.addEventListener('DOMContentLoaded', function() {
    const calendarEl = document.getElementById("calendar");

    let calendar = new Calendar(calendarEl, {
        locale: itLocale,
        plugins: [ dayGridPlugin, timeGridPlugin, listPlugin ],
        initialView: 'dayGridMonth',
        headerToolbar: {
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek,listWeek'
        },
        // go ahead with other parameters
    });

    calendar.render();
});
  1. In my view file:
<div id="calendar"></div>
  1. In my footer section:
<script src="{{ asset('js/app.js') }}"></script>
  1. Run npm run dev (or prod or watch, it depends)

Final note: I used DOMContentLoaded because my app.js script it's in footer. I don't know if this works also if is placed in head tag.

Anett answered 14/4, 2021 at 14:19 Comment(1)
And how to add the css correctly?Kalisz
D
0

You can wrap the instantiation to use default options and then merge with the new options.

import { Calendar } from '@fullcalendar/core'
import allLocales from '@fullcalendar/core/locales-all'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'

(function (global) {
  global.FullCalendar = {
    Calendar: function (elt, options) {
      return new Calendar(elt, Object.assign({
        plugins: [ dayGridPlugin, timeGridPlugin ],
        locales: allLocales,
      }, options))
    }
  }
})(window)

Then you can use it like this


const calendarEl = document.getElementById("calendar")
    
let calendar = new FullCalendar.Calendar(calendarEl, {
                                locale: 'fr',
                                initialView: 'dayGridMonth',
                                headerToolbar: {
                                    left: 'prev,next today',
                                    center: 'title',
                                    right: 'dayGridMonth,timeGridWeek,listWeek'
                                }
                            })

calendar.render()
    

Credit to Bartheleway on Github
Source

Dennet answered 17/3, 2022 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.