【问题标题】:What is wrong with my import/export class code lines?我的导入/导出类代码行有什么问题?
【发布时间】:2019-04-17 20:12:17
【问题描述】:

我正在开发一个破砖游戏,虽然我的代码可以在 CodeSandbox 上运行,但当我在本地计算机上尝试它时,导入功能不起作用,游戏也随之不起作用。

我尝试移动文件夹,研究了如何移动,并尝试在类名周围添加 {},但无济于事。

这是我的 index.html 脚本链接

<script src="src/index.js"></script>

这是在我的 index.js 文件中(在文件夹“src”中)

import Game from "/src/game.js";

这就是我的 game.js 文件中的“游戏”(也在文件夹“src”中)。

export default class Game {
  constructor(gameWidth, gameHeight) {
    this.gameWidth = gameWidth;
    this.gameHeight = gameHeight;
    this.gamestate = GAMESTATE.MENU;
    this.paddle = new Paddle(this);
    this.ball = new Ball(this);
    this.gameObjects = [];
    new InputHandler(this.paddle, this);
  }

在 CodeSandbox 中,游戏没有问题,因为它运行正常,而当我在本地运行它时,控制台显示“SyntaxError: Unexpected identifier 'Game'。import call 只需要一个参数。”画布是空白的。我已经被困了一段时间,真的需要一些帮助。

【问题讨论】:

  • 尝试用括号括起来类声明:export default (class Game { ... })
  • 游戏路径是否正确?应该是"./game.js";

标签: javascript import html5-canvas export


【解决方案1】:
import Game from './game';

在 index.js 文件中导出模块更为常见 因此,您可以尝试以下方法:

export Game from './game';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2020-11-29
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多