我试图理解 JavaScript 中的module,但是当我尝试编写一个非常简单的module时,我收到 CORS 错误。我使用括号作为我的文本编辑器,奇怪的部分是当我在括号中使用实时预览时,代码有效,但是当我通常打开.js
我收到错误的文件。
索引.html
<!DOCTYPE html>
<html>
<head>
<title> JS </title>
</head>
<body>
<script type="module">
import {add} from './libA.js';
console.log(add(10,20));
</script>
</body>
</html>
libA.js
export function add(a,b) {
return a+b ;
}
*我收到此错误->
Access to Script at 'file:///F:/WEB%20DEV/JavaScript/libA.js' from origin `null` has been blocked by CORS policy:
Invalid response.
Origin 'null' is therefore not allowed access.
(我什至也尝试了最新版本的 chrome)