服务器响应 MIME 类型 "image/png" - HTML / JavaScript

the server responded with a MIME type of "image/png" - HTML / JavaScript

我正在使用 HTML 和 JavaScript 构建类似马里奥的游戏,现在快完成了,我想通过图像更改我的矩形(平台)。

这是我的结构:

- index.html
- index.js
- index.css
- /assets/platform.png

要导入我的图像,在我的 index.js 中我做了: import platform from './assets/platform.png'

浏览器return这个错误:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "image/png". Strict MIME type checking is enforced for module scripts per HTML spec.

我正在使用来自 VSCode 的实时服务器。

有什么帮助吗?

导入用于导入脚本。你导入图片。

您可以使用 add img src 属性动态地使用 js:

document.querySelector('.myImg').src = 'https://placekitten.com/500/500' // or ./assets/platform.png
<img class="myImg"/>