【问题标题】:SyntaxError: Cannot use import statement outside a module -- jspdfSyntaxError: 不能在模块外使用 import 语句——jspdf
【发布时间】:2021-08-06 14:27:40
【问题描述】:

我一直在尝试创建一个使用 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>

【问题讨论】:

    标签: javascript html syntax-error jspdf


    【解决方案1】:

    请参阅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 还是很陌生。
    • @Ernsto Stifano,也为我工作,谢谢!
    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2020-12-31
    • 1970-01-01
    • 2022-07-19
    相关资源
    最近更新 更多