简体   繁体   English

JavaScript 类型模块不执行

[英]JavaScript type module doesn't execute

I have below html file:我有以下 html 文件:

<!DOCTYPE html> 
<html lang="en"> 

<head> 

</head> 

<body> 

    <script type="module">
        console.log("test");
    </script>

</body>
</html>

However, the console doesn't print out anything.但是,控制台不会打印任何内容。 If I remove the type="module" , it works well.如果我删除type="module" ,它运行良好。 The version of Chrome I use is 58.0.3029.81.我使用的 Chrome 版本是 58.0.3029.81。 I also googled and tested my current Chrome supports ES6.我还用谷歌搜索并测试了我当前的 Chrome 支持 ES6。

Any idea why?知道为什么吗?

It's not supported in version 58. https://caniuse.com/#search=type%3D%22module%22版本 58 不支持它。 https://caniuse.com/#search=type%3D%22module%22

But you can use nomodule to compatible with it.但是你可以使用nomodule来兼容它。

The nomodule attribute is a boolean attribute that prevents a script from being executed in user agents that support module scripts. nomodule 属性是一个布尔属性,可防止在支持模块脚本的用户代理中执行脚本。 This allows selective execution of module scripts in modern user agents and classic scripts in older user agents, as shown below.这允许有选择地执行现代用户代理中的模块脚本和旧用户代理中的经典脚本,如下所示。 The nomodule attribute must not be specified on module scripts (and will be ignored if it is).不得在模块脚本上指定 nomodule 属性(如果是,则将被忽略)。

Here's a html.spec.whatwg.org 's example这是html.spec.whatwg.org的示例

This example shows how to include a module script for modern user agents, and a classic script for older user agents:此示例展示了如何为现代用户代理包含一个模块脚本,以及如何为旧用户代理包含一个经典脚本:

<script type="module" src="app.mjs"></script>
<script nomodule defer src="classic-app-bundle.js"></script>

In modern user agents that support module scripts, the script element with the nomodule attribute will be ignored, and the script element with a type of "module" will be fetched and evaluated (as a module script).在支持模块脚本的现代用户代理中,具有 nomodule 属性的脚本元素将被忽略,并且具有“模块”类型的脚本元素将被获取和评估(作为模块脚本)。 Conversely, older user agents will ignore the script element with a type of "module", as that is an unknown script type for them — but they will have no problem fetching and evaluating the other script element (as a classic script), since they do not implement the nomodule attribute.相反,较旧的用户代理将忽略具有“模块”类型的脚本元素,因为这对他们来说是未知的脚本类型——但他们在获取和评估其他脚本元素(作为经典脚本)时没有问题,因为它们不实现 nomodule 属性。

It means in higher than version 61 type=module and nomodule is supported, then:这意味着在高于版本 61 type=modulenomodule被支持,那么:

  1. The script will be executed in script element with type of "module".该脚本将在类型为“模块”的脚本元素中执行。
  2. The script will be not executed in script element with attribute of "nomodule".该脚本将不会在具有“nomodule”属性的脚本元素中执行。

And in lower than version 60 type=module and nomodule isn't supported, then:在低于版本 60 type=module并且不支持nomodule情况下,则:

  1. The script will be not executed in script element with type of "module", because type=module is an unknown script type for the browser.该脚本不会在类型为“module”的脚本元素中执行,因为type=module是浏览器的未知脚本类型。
  2. The script will be executed in script element with attribute of "nomodule", because the browser doesn't implement the "nomodule" attribute.脚本将在具有“nomodule”属性的脚本元素中执行,因为浏览器没有实现“nomodule”属性。

Hope that can help you.希望能帮到你。

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

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