![](/img/trans.png)
[英]How to Import Javascript Module from external javascript file into <script> tag of HTML file
[英]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.