【发布时间】:2023-03-20 12:38:01
【问题描述】:
我尝试将我的 Java 类 Yellow.js 导入我的 index.js 并获取
“未捕获的语法错误意外标识符”
经过一些谷歌搜索后,我得到了改变我的建议
<script src="src/index.js"></script>
进入
<script type="module" src="src/index.js"></script>
但这会导致:
从源“null”访问“file:///D:/Game/src/index.js”处的脚本 已被 CORS 策略阻止:跨源请求仅 支持协议方案:http、data、chrome、chrome-extension、 https
我的代码现在看起来像这样:
index.html
<html>
<head>
<title>Title</title>
<meta charset="UTF-8">
<style>
#gameScreen {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="gameScreen" width='600' height='600'></canvas>
<script type="module" src="src/index.js"></script>
</body>
</html>
index.js
import Yellow from "/src/yellow";
let canvas = document.getElementById("gameScreen");
let ctx = canvas.getContext("2d");
let yellow = new Yellow();
yellow.draw();
黄色.js
export default class yellow{
draw(ctx) {
ctx.fillStyle = '#ff0';
ctx.fillRect(20, 20, 100, 50);
}
}
我错过了什么吗?
我真的不明白如何在哪里添加 "type="module" 东西。
【问题讨论】:
-
这导致此错误:CORS 策略已阻止从源“null”访问“file:///D:/Game/src/index.js”处的脚本:跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。
标签: javascript ecmascript-6 import module