gpt4 book ai didi

javascript - 语法错误 : Cannot use import statement outside a module -- jspdf

转载 作者:行者123 更新时间:2023-12-05 02:42:31 26 4
gpt4 key购买 nike

我一直在尝试创建一个使用 jspdf 模块导出 pdf 文件的 html 和 javascript 应用程序。

每次我运行下面的代码时,我都会得到错误Uncaught SyntaxError: Cannot use import statement outside a module

    <!DOCTYPE html>
<html>
<head>
<title>Test 2</title>
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js">

</script>

<script type="text/javascript">
import { jsPDF } from "jspdf";

// Default export is a4 paper, portrait, using millimeters for units
const doc = new jsPDF();

doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");
alert("hello")
</script>


</head>
<body>

</body>
</html>

最佳答案

请参阅 NPM Package Page 中的“其他模块格式”>“全局变量” .

您不能在 ES6 module 之外使用 import 语句.通常,脚本标签将 type 属性设置为 module

<!DOCTYPE html>
<html>
<head>
<title>Test 2</title>
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js">

</script>

<script type="text/javascript">
// Default export is a4 paper, portrait, using millimeters for units
const { jsPDF } = window.jspdf;

const doc = new jsPDF();

doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");
alert("hello")
</script>


</head>
<body>

</body>
</html>

关于javascript - 语法错误 : Cannot use import statement outside a module -- jspdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67567775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com