Ecmascript 6 es6导入变量未在代码导入中定义
出于某种原因,当我执行var sphere=new Core()时;在游戏中,我看到核心是未定义的,即使我导入了它: Game.jsEcmascript 6 es6导入变量未在代码导入中定义,ecmascript-6,traceur,Ecmascript 6,Traceur,出于某种原因,当我执行var sphere=new Core()时;在游戏中,我看到核心是未定义的,即使我导入了它: Game.js import Core from 'gameUnits/Core' export class Game { constructor() { Core.js: export class Core { constructor(scene) { } } 当您在没有花括号的情况下进行导入时,您正在尝试导入模块的默认对象 因此,
import Core from 'gameUnits/Core'
export class Game {
constructor() {
Core.js:
export class Core {
constructor(scene) {
}
}
当您在没有花括号的情况下进行导入时,您正在尝试导入模块的默认对象 因此,您必须将
default
关键字添加到Core
导出:
export default class Core {
constructor(scene) {
}
}
或将您的核心
导入放在花括号中:
import { Core } from 'gameUnits/Core';
查看有关ECMAScript 6模块的更多信息
PS:使用default
关键字,可以为Core
类指定任何名称。例如:
import GameUnitsCore from 'gameUnits/Core';