es6 import var not defined in code importing

六眼飞鱼酱① 提交于 2019-12-10 17:25:05

问题


For some reason when I do var sphere = new Core(); in Game, I see Core is undefined, even though I import it:

Game.js

  import Core from 'gameUnits/Core' 

    export class Game { 
    constructor() {

Core.js:

export class Core {
    constructor(scene) {
    }
}

回答1:


When you make import without curly brackets you're trying to import default object of the module.

So, you must add default keyword to your Core exporting:

export default class Core {
    constructor(scene) {
    }
}

OR place your Core importing into curly brackets:

import { Core } from 'gameUnits/Core';

Look here for more informaction about ECMAScript 6 modules

PS: Using default keyword you can specify ANY name for Core class. For example:

import GameUnitsCore from 'gameUnits/Core';


来源:https://stackoverflow.com/questions/27741566/es6-import-var-not-defined-in-code-importing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!