如何导入在html内的<script type="module">标签中定义的es6模块?
How to import es6 module that has been defined in <script type="module"> tag inside html?
我可以在我的 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>
另见
=> 是否可以将 "anonymous" 模块导入同一 html 文件中的另一个模块脚本?或者某些 "code behind"- JavaScript 文件也已被 me.html 文件加载?导出似乎有效;至少它不会抛出任何错误。
对于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';
但是,其中 none 行有效。
=>如果可能的话,引用 "anonymous" 模块的正确语法是什么?
我使用Chrome版本63.0.3239.108.
相关问题:
据我所知,无法导入 "anonymous" 模块,因为 "anonymous" 模块没有模块说明符或单独的 url(它的 import.meta.url
只是html url 作为当前规范)。理论上它可以在未来扩展,但我找不到这个功能的好用例。
我可以在我的 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>
另见
=> 是否可以将 "anonymous" 模块导入同一 html 文件中的另一个模块脚本?或者某些 "code behind"- JavaScript 文件也已被 me.html 文件加载?导出似乎有效;至少它不会抛出任何错误。
对于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';
但是,其中 none 行有效。
=>如果可能的话,引用 "anonymous" 模块的正确语法是什么?
我使用Chrome版本63.0.3239.108.
相关问题:
据我所知,无法导入 "anonymous" 模块,因为 "anonymous" 模块没有模块说明符或单独的 url(它的 import.meta.url
只是html url 作为当前规范)。理论上它可以在未来扩展,但我找不到这个功能的好用例。