索引.html
<html>
<head>
<script type="module">
import {answer} from './code.js'
console.info("It's ${answer()} time!")
</script>
</head>
<body>
</body>
</html>
代码.js
export function answer(){
return 'module';
}
Error: Access to Script at 'file:///C:*******/es6/code.js' from origin 'null' has been blocked by CORS policy: Invalid response. Origin 'null' is therefore not allowed access.
Chrome 说它可以支持模块,并且我已经在网络上看到了运行的示例,但是当我复制下载并在本地运行它们时,我总是收到上面的错误。我不想使用 Babel、Webpack 等。
我尝试在 Chrome 和 Chrome Canary 中启用实验性 Web 平台功能标志。
最佳答案
与常规脚本不同,ES6 模块受 same-origin policy 的约束。这意味着如果没有 CORS header (无法为本地文件设置),您就无法从文件系统或跨源导入
它们。
基本上,您需要从(本地)服务器运行此代码或在浏览器中禁用同源以进行测试(不要永久执行此操作)。请参阅:Access to Image from origin 'null' has been blocked by CORS policy .
关于javascript - Chrome 62/Chrome Canary 64 中的 ES6 模块支持,在本地不起作用,CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992463/