【问题标题】:How use export in an HTML如何在 HTML 中使用导出
【发布时间】:2018-06-01 19:29:33
【问题描述】:

当我有带有导出语句的 extern javascript 文件时,如何在 HTML 中调用它?

  • <script> 标记中的导入也不起作用。
  • <head> 上的包含给我一个“意外的令牌导出”错误

例如: 我的外部 js 文件

export function myFunction(text) { return text; }

我的 HTML:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="myExternFile.js"> </script>
</head>
<body>
    <script>
        console.log(myFunction("some text"));
    </script>
</body>
</html>

【问题讨论】:

  • 这不是export 的预期行为,您也不需要export 进行此类函数调用。删除export,您的代码必须可以正常工作。
  • 这个问题可能与this question重复。

标签: javascript html


【解决方案1】:

根据ECMAScript modules in browsers,你可以用&lt;script type="module"&gt;做到这一点

<script type="module">
  import {myFunction} from './myExternalFile.js';

  console.log(myFunction("some text"));
</script>

【讨论】:

    【解决方案2】:

    您的函数将在没有导出的情况下工作。只需确保在脚本标签之间调用它即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      相关资源
      最近更新 更多