Поэтому, когда я пытаюсь импортировать класс из другого файла javascript, я получаю сообщение об ошибке в консоли, например:
Access to Script at 'file:///home/../.../JavaScript/src/index.js' from origin 'null' has been blocked by CORS policy: Invalid response. Origin 'null' is therefore not allowed access.
В моем html-файле я добавляю файл сценария следующим образом:
<script type="module" src="src/index.js"></script>
Мой index.js выглядит следующим образом:
import Paddle from "/src/paddle";
let canvas = document.getElementById("gameScreen");
let ctx = canvas.getContext("2d");
const GAME_WIDTH = 800;
const GAME_HEIGHT = 600;
ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
let paddle = new Paddle(GAME_WIDTH, GAME_HEIGHT);
paddle.draw(ctx);
И paddle.js:
export default class Paddle {
constructor(gameWidth, gameHeight){
this.width = 150;
this.height = 30;
this.position = {
x: gameWidth/2 - this.width/2,
y: gameHeight-this.height-10
}
}
draw(ctx){
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
Я использую браузер Chromium.И моя структура папок выглядит так:
/javascript
-/src
-index.js
-paddle.js
-index.html
Кто-нибудь знает, как избежать политики Cors?