Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

javascript - import class and call static method with es6 modules with babel transpiler

I have the following class definition:

class EmberReflux{
  static createActions(actions) {
    console.log(actions);
  }
}

export { EmberReflux };

When I import it from a different file:

import EmberReflux from '../utils/ember-reflux';

let TodoActions = EmberReflux.createActions(
[
  "addItem",
  "undo",
  "redo"
]);

export { TodoActions };

The transpiled looks like this

define('ember-reflux/utils/todo-actions', ['exports', 'ember-reflux/utils/ember-reflux'], function (exports, EmberReflux) {

    'use strict';

    var TodoActions = EmberReflux['default'].createActions(["addItem", "undo", "redo"]);

    exports.TodoActions = TodoActions;

});

I'm not sure what the default is in EmberReflux['default']

I want to call the static class method like this:

EmberReflux.createActions

But instead I have to call it like this:

EmberReflux.EmberReflux.createActions
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two options:

  1. Export EmberReflux like you are doing:

    export { EmberReflux };
    

    and then import it like:

    import { EmberReflux } from '../utils/ember-reflux';
    
  2. Use default when exporting:

    export default EmberReflux;
    

    and import it (like you are doing):

     import EmberReflux from '../utils/ember-reflux';
    

In both cases you can then use your EmberReflux like:

EmberReflux.createActions();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...