es6 import var not defined in code importing

2019-06-24 15:08发布

问题:

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';