简体   繁体   English

如何在HTML中使用导出

[英]How use export in an HTML

When I have extern javascript file with an export-statement how can I call this in an HTML ? 当我有带有导出语句的外部javascript文件时,如何在HTML中调用它?

  • An import in a <script> tag also doesn't work. <script>标记中的导入也不起作用。
  • an include over the <head> gives me a "Unexpected token export" error <head>的include给我一个“意外的令牌导出”错误

For example: my extern js-File 例如:我的extern js-File

export function myFunction(text) { return text; }

My HTML: 我的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>

According to ECMAScript modules in browsers you can do it with <script type="module"> 根据浏览器中的ECMAScript模块,您可以使用<script type="module">

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

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

Your function will work without the export. 您的功能将在不导出的情况下运行。 Just make sure to call it between the script tags. 只要确保在脚本标签之间调用它即可。

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

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