ES6 - Exporting module with a getter

后端 未结 2 580
走了就别回头了
走了就别回头了 2021-01-18 09:56

would like to export a module that get\'s the module\'s definition from some global object.

It\'s something like:

export {
  get DynamicModule() {
           


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 10:23

    No, it is impossible to make a getter for a module export - they are variable bindings, not properties.

    However you could simply make that a default export:

    export default __globalFluxStorage.state.property.property.property.property;
    

    import DynamicModule from 'dynamic-module';
    

    If you want a named import, you'll have to declare the name in your export:

    export var DynamicModule = __globalFluxStorage.state.property.property.property.property;
    

    import {DynamicModule} from 'dynamic-module';
    

    This would also allow changing the value later when it's not available right at the time of the module being loaded:

    export var DynamicModule;
    …
    DynamicModule = __globalFluxStorage.state.property.property.property.property;
    

    (although in that case you might want to consider exporting a Promise or EventEmitter instead)

提交回复
热议问题