ES6 - 带有吸气剂的导出模块

时间:2021-10-23 12:36:27

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

想从某个全局对象导出一个获取模块定义的模块。

It's something like:

它是这样的:

export {
  get DynamicModule() {
    return __globalFluxStorage.state.property.property.property.property
  }
}

...

import {DynamicModule} from 'dynamic-module'

We have a complex flux storage and DynamicModule is just a means of accessing __globalFluxStorage.state.property.property.property.property without the need to type in the long property accessor. Is this possible? Thanks.

我们有一个复杂的通量存储,而DynamicModule只是一种访问__globalFluxStorage.state.property.property.property.property的方法,无需输入long属性访问器。这可能吗?谢谢。

Edit:

编辑:

Since I am using babel, tried something like this:

因为我正在使用babel,尝试过这样的事情:

Object.defineProperty(module.exports, "Forms", {
  get: function() {
    return __globalFluxStorage.state.property.property.property.property
  }
});

But does not work, i.e. {DynamicModule} is undefined

但是不起作用,即{DynamicModule}未定义

2 个解决方案

#1


4  

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

不,不可能为模块导出制作一个getter - 它们是变量绑定,而不是属性。

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)

(尽管在这种情况下你可能想要考虑导出Promise或EventEmitter)

#2


0  

You can export an object with a getter or export a function if you need to re-evaluate the value every time it is used in imports.

如果需要在每次在导入中使用时重新评估该值,则可以使用getter导出对象或导出函数。

export const _ = {
  get DynamicModuleGetter() {return __globalFluxStorage.state.property.property.property.property}
}

export function DynamicModuleFunction() {return __globalFluxStorage.state.property.property.property.property}

Then in import

然后在导入

import { _, DynamicModuleFunction } from 'dynamic-module'

// getter
const value1 = _.DynamicModuleGetter
const {DynamicModuleGetter} = _        // this evaluates the getter

// function
const value2 = DynamicModuleFunction()

A more elaborate example

let obj = {
  foo: {
    bar: {
      baz: {
        bak: {
          value: 1
        },
        fak: {
          value: 2
        }
      }
    }
  }
}

export const _ = {
  get shortcut() {return obj.foo.bar.baz}
}

export function shortcut() {return obj.foo.bar.baz}

import

进口

import { _, shortcut } from './shortcut'

let g = _.shortcut.bak.value       // g = 1
let f = shortcut().fak.value       // f = 2
let {shortcut: {bak: {value}}} = _ // value = 1

#1


4  

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

不,不可能为模块导出制作一个getter - 它们是变量绑定,而不是属性。

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)

(尽管在这种情况下你可能想要考虑导出Promise或EventEmitter)

#2


0  

You can export an object with a getter or export a function if you need to re-evaluate the value every time it is used in imports.

如果需要在每次在导入中使用时重新评估该值,则可以使用getter导出对象或导出函数。

export const _ = {
  get DynamicModuleGetter() {return __globalFluxStorage.state.property.property.property.property}
}

export function DynamicModuleFunction() {return __globalFluxStorage.state.property.property.property.property}

Then in import

然后在导入

import { _, DynamicModuleFunction } from 'dynamic-module'

// getter
const value1 = _.DynamicModuleGetter
const {DynamicModuleGetter} = _        // this evaluates the getter

// function
const value2 = DynamicModuleFunction()

A more elaborate example

let obj = {
  foo: {
    bar: {
      baz: {
        bak: {
          value: 1
        },
        fak: {
          value: 2
        }
      }
    }
  }
}

export const _ = {
  get shortcut() {return obj.foo.bar.baz}
}

export function shortcut() {return obj.foo.bar.baz}

import

进口

import { _, shortcut } from './shortcut'

let g = _.shortcut.bak.value       // g = 1
let f = shortcut().fak.value       // f = 2
let {shortcut: {bak: {value}}} = _ // value = 1