使用Rollupjs,我尝试在index.html页面上运行模块
<script type="module" src="./bundle.js" ></script>
<script>
window.addEventListener('load', () => {
const lib = new MyLibrary();
console.log(lib);
});
</script>
并且出现以下错误:
((index):17 Uncaught ReferenceError:未定义MyLibrary
模块格式为UMD。谢谢!
如何确保在脚本之前加载模块。我认为您应该尝试使用async
或defer
标志加载第一个脚本。例如:
带有defer
<script defer type="module" src="./bundle.js"></script>
<script>
window.addEventListener('load', () => {
const lib = new MyLibrary();
console.log(lib);
});
</script>
带有async
<script async type="module" src="./bundle.js"></script>
<script>
window.addEventListener('load', () => {
const lib = new MyLibrary();
console.log(lib);
});
</script>