ES6模块:重新导出为对象

时间:2015-12-09 08:50:46

标签: javascript module ecmascript-6

我有moduleA导出一些函数:

// moduleA.js
export function f1() {...}
export function f2() {...}

有没有办法在moduleB中重新导出moduleA的所有导出,并使其看起来像一个对象:

// moduleB.js
export * as a from 'moduleA';  // pseudo code, doesn't work

这样我才能以这种方式使用它?

// main.js
import {a} from 'moduleB';
a.f1();
a.f2();

2 个答案:

答案 0 :(得分:23)

语法尚不支持,但有a proposal for it

您现在可以将它与Babel.js一起使用,或者只是执行:

import * as a from '...';
export {a};

答案 1 :(得分:-1)

<强> file1.js

export let uselessObject = {
  con1 : function () {
    console.log('from file1.js')
  }
}

<强> file2.js

import { uselessObject } from './file1.js'

uselessObject.con2 = function(){
  console.log('from file2.js ')
}

export { uselessObject } from './file1.js'

<强> Index.js

import { uselessObject } from './file2.js'
uselessObject.con1()
uselessObject.con2()

<强>输出

from file1.js
from file2.js