我可以在我的 html 文件 me.html 中定义一个模块:
<script type="module" id="DEFAULT_MODULE">
import Atom from './atom.js';
console.log("definition of getAtom")
export default function getAtom(){
return new Atom('atom');
}
console.log("exported getAtom")
</script>
另见
=> 是否可以将该“匿名”模块导入同一 html 文件中的另一个模块脚本?还是某些“代码隐藏”- 也已由 me.html 文件加载的 JavaScript 文件?导出似乎有效;至少它不会抛出任何错误。
对于 getAtom
方法的导入,我试过例如:
<script type="module">
import getAtom from '.'; //this line does not work
console.log("usage of getAtom")
var atom = getAtom();
</script>
我希望有一些像
这样的语法 import getAtom;
import getAtom from '.';
import getAtom from window;
import getAtom from './me.html';
import getAtom from '.DEFAULT_MODULE';
但是,这些行都不起作用。
=>如果可能的话,引用“匿名”模块的正确语法是什么?
我使用的是 Chrome 版本 63.0.3239.108。
相关问题:
最佳答案
据我所知,没有办法导入“匿名”模块,因为“匿名”模块没有模块说明符或单独的 url(它的 import.meta.url
只是 html url 作为当前规范)。理论上它可以在未来扩展,但我找不到这种功能的好的用例。
关于javascript - 如何导入已在 <script type ="module"> 标签中定义的 es6 模块 inside html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47982205/