【问题标题】:Re-Exporting entire module in ES6/Babel在 ES6/Babel 中重新导出整个模块
【发布时间】:2016-09-29 21:01:33
【问题描述】:

假设我有一个要重新导出的模块:

//exportme.js
export default 'EXPORTME';
export const test = () => console.log('test function');

//reexport.js
export * from './exportme.js'

当我导入 reexport.js 时,exportme.js 的默认值不可用。

//app.js
import reexport from './reexport.js'

console.log(reexport) //undefined

我必须将 reexport.js 设置为以下内容才能正常工作。

export * from './exportme.js'
export default from './exportme.js'

有没有更简单的方法可以做到这一点,或者可以将其合并为一个语句?

export default, * from './exportme.js' 不起作用。

我正在使用最新的 babel 和 transform-export-extensions

【问题讨论】:

标签: javascript ecmascript-6 babeljs es6-module-loader es6-modules


【解决方案1】:

exportme.js 的默认值不可用

是的,星型导出不会重新导出默认导出。 export * from … 的目的是允许从多个模块重新导出,从多个模块导出 default 只会导致冲突。因此,您必须明确指定它(如果您需要它,通常在命名导出旁边没有默认导出)。

有没有更简单的方法可以做到这一点,或者可以将其合并到一个语句中?

不,你有两条线是要走的路。

【讨论】:

    【解决方案2】:

    正如 Bergi 所写,没有办法在一行中使用 ES 6 导出来做到这一点。但是,您可以简单地要求您要重新导出的模块并将结果分配给module.exports

    module.exports = require('./exportme.js')
    

    【讨论】:

    • 这很聪明并且有效。不幸的是,我试图坚持不使用 require!
    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 2021-11-27
    • 2016-06-07
    • 2016-12-11
    • 2016-11-15
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    相关资源
    最近更新 更多