未捕获的SyntaxError:当我导出和导入类时,无法在模块外使用import语句

SpeedoThreeSixty

因此,我准备使用纯编码制作沙盒游戏,并且我想为Block测试代码中调用的类制作一个单独的文件这是代码:

package.json

{
    "name": "speedbox",
    "description": "SpeedBox is a simple SandBox game owned by Dean Summer (SpeedoThreeSixty).",
    "version": "1.0.0",
    "main": "index.html",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/JavascriptLearner815/SpeedBox.git"
    },
    "keywords": ["speedo", "speedothreesixty", "sandbox", "box", "speed", "speedbox"],
    "author": "Dean Summer <[email protected]>",
    "license": "MIT",
    "bugs": {
        "url": "https://github.com/JavascriptLearner815/SpeedBox/issues"
    },
    "homepage": "https://github.com/JavascriptLearner815/SpeedBox"
}

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="SpeedBox is a simple SandBox game owned by Dean Summer (SpeedoThreeSixty).">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>SpeedBox - Home</title>
        <link rel="stylesheet" href="./main.css">
        <script defer src="block.js" type="module"></script>
        <script defer src="index.js" type="module"></script>
    </head>
    <body>
        <button id="createBlock">Create a block!</button>
    </body>
</html>

main.css为空

index.js

try {
    import { Block } from "./block";
    globalThis.blocks = [];
    var createBlockButton = document.getElementById("createBlock");
    createBlockButton.addEventListener("click", () => {
        const newBlock = new Block(1);
        blocks.push(newBlock);
    });
} catch (error) {
    console.error(error);
}

block.js

export default class Block {
    constructor(id) {
        try {
            if (globalThis.blocks.length === 5) throw "Cannot exceed block limit";
            this.id = id;
            if (this.id === 1) this.type = "grass";
            console.log(`Created block of type ${this.type}`);
        } catch (error) {
            console.error(error);
        }
    }
}

无论我如何尝试,我总是收到以下错误消息之一:Uncaught SyntaxError:无法在模块index.js之外使用import语句:2 Uncaught SyntaxError:意外的令牌'{'index.js:2 Uncaught SyntaxError:意外的标识符索引。 js:2

确实有Node,但是在此示例中我不想使用它。另外,该问题在过去已经发生。

贝吉

错误消息有点令人困惑,但是您不能importtry使用声明它必须在模块代码的顶层:

import { Block } from "./block"; /*
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
try {
    globalThis.blocks = [];
    var createBlockButton = document.getElementById("createBlock");
    createBlockButton.addEventListener("click", () => {
        const newBlock = new Block(1);
        blocks.push(newBlock);
    });
} catch (error) {
    console.error(error);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SyntaxError:无法在模块外部使用import语句

Javascript模块SyntaxError:无法在模块外部使用import语句

使用TypeScript和nodemon:SyntaxError:无法在模块外部使用import语句

“未捕获的SyntaxError:无法在模块外部使用import语句”-这是什么意思?

Javascript模块错误SyntaxError:无法在模块外部使用import语句

使用Vue Js运行Jest测试时,出现“ SyntaxError:无法在模块外部使用import语句”

Babel,Jest和webpack出现“ SyntaxError:无法在模块外部使用导入语句”

未捕获的SyntaxError:无法在模块外部使用import语句

尝试在Django应用程序的js文件中导入vue.js时出现“未捕获的SyntaxError:无法在模块外部使用import语句”

npm模块出现“ SyntaxError:无法在模块外部使用导入语句”

未捕获的SyntaxError:使用reactjs CDN链接的模块外部无法使用import语句

未捕获的SyntaxError:无法在节点js中的模块外部使用import语句

SyntaxError:无法使用TypeScript在模块Firebase部署错误之外使用import语句

如何修复“ SyntaxError:无法在模块外部使用导入语句”

导入ReactJS时出现“未捕获的SyntaxError:无法在模块外部使用import语句”

导入“ uuid”时无法在模块外部使用import语句

当我使用 numpy 时,它说 - 未使用的导入语句。如何修复它?

不能在模块外使用 import 语句

未捕获的语法错误:无法在模块 Vue.js 单组件文件外使用 import 语句

WebdriverIO 和 Angular,在导入类的打字稿中编写 e2e 测试(不能在模块外使用 import 语句)

SyntaxError:不能在模块外使用导入语句 - vue电子构建器插件

使用angularjs运行业力单元测试时如何解决“无法在模块外使用导入语句”?

导入模块时不能在模块外使用 import 语句

Node12/next9 SyntaxError: 不能在模块外使用 import 语句

“未捕获的语法错误:无法在模块外使用导入语句”reactjs 中的错误

SyntaxError:在遵循 vue-test-utils 官方教程时不能在模块外使用 import 语句

“不能在模块外使用导入语句”当我尝试 mocha 时收到消息

SyntaxError:在使用“fast-image-zoom”时不能在模块外使用 import 语句

模块为 CommonJS 时 ts-node 中的“SyntaxError:无法在模块外使用导入语句”错误