es6 import var not defined in code importing

2019-06-24 14:43发布

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条回答
三岁会撩人
2楼-- · 2019-06-24 15:12

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';
查看更多
登录 后发表回答