我可以从其他 <script type="module"> 导入 ES 模块吗

Can I import ES Modules from other <script type="module">

我想从另一个脚本标签导入。

<script type="module">
    export default "str"
</script>

<script type="module">
    import foo from "????"
    console.log(foo) // str
</script>

我应该在“???”中写什么?

这不可能。 ModuleSpecifier(from 之后的内容)必须是字符串,并且需要指向带有导出的 独立 JavaScript 文件 。它不能引用页面上的另一个脚本标签,只能引用一个文件。

通常,页面本身的一个<script type="module">只使用一次,用于调用其他模块。例如,您通常会看到或拥有类似以下内容的内容,而不是您的原始代码:

<script type="module">
    import foo from "str.js"
    console.log(foo) // str
</script>
// str.js
export default "str"