繁体   English   中英

在 JavaScript 中,我们可以在<script></script>标签而不加载外部文件?

[英]In JavaScript, can we import a JS module in the <script></script> tag without loading an external file?

客户端,我们可以在 HTML 页面的<script></script>标签中导入 JavaScript 模块而不加载外部脚本吗?

<html>
  <head>
  ...
  </head>
  <body>
  ...
    <script>
      document.getElementById('btn').onclick = () => { 
        // Import a JS module here
      }
    </script>
  </body>
</html>

不要使用import * as label from "file.js" 使用以下代码:

<html>
  <head>
  ...
  </head>
  <body>
  ...
    <script>
      document.getElementById('btn').onclick = () => { 
        // Import a JS module here
        import("file.js").then(obj => {
          // Do something with obj
        })
      }
    </script>
  </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM