[英]Dynamic import with multiple modules
我之前有过这段代码,它运行良好(仅在此处显示导入):
import { Calendar } from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid';
我以为我可以动态加载它们,但我认为我做得不对。 我改成这样:
let calendarEl = document.getElementById('fullcalendar');
if(calendarEl) {
Promise.all([
import(/* webpackChunkName: 'fullcalendar-core' */ '@fullcalendar/core'),
import(/* webpackChunkName: 'fullcalendar-daygrid' */ '@fullcalendar/daygrid')
])
.then(([{Calendar}, dayGridPlugin]) => {
let calendar = new Calendar(document.getElementById('fullcalendar'), {
plugins: [dayGridPlugin]
});
calendar.render();
})
.catch(console.warn);
我的日历没有加载,错误信息是: Cannot read properties of undefined (reading 'length')
,它指向let calendar = new Calendar
行。
这个最后对我有用:
let calendarEl = document.getElementById('fullcalendar');
if(calendarEl) {
(async () => {
const {Calendar} = await import('@fullcalendar/core');
const {default: dayGridPlugin} = await import('@fullcalendar/daygrid');
let calendar = new Calendar(calendarEl, {
plugins: [dayGridPlugin],
initialView: 'dayGridMonth'
});
calendar.render();
})();
}
import dayGridPlugin from …
声明是默认导入。 所以你需要使用
.then(([{Calendar}, {default: dayGridPlugin}]) => {
const calendar = new Calendar(document.getElementById('fullcalendar'), {
plugins: [dayGridPlugin]
});
calendar.render();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.