简体   繁体   English

ES6模块:重新导出为对象

[英]ES6 module: re-export as object

I have moduleA which exports some functions: 我有moduleA导出一些函数:

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

Is there any way to re-export all exports of moduleA in moduleB and make it looks like one object: 有没有办法在moduleB中重新导出moduleA的所有导出并使其看起来像一个对象:

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

So that I can use it in this way? 这样我就可以用这种方式使用它?

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

The syntax is not supported yet but there is a proposal for it . 语法尚不支持,但有一个提案

You can use it now with Babel.js or simply do: 您现在可以使用它与Babel.js或只是做:

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

file1.js file1.js

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

file2.js file2.js

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

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

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

Index.js Index.js

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

Output 产量

from file1.js
from file2.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM